mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-07-17 03:30:32 +08:00
Create 1079.延迟的回文数.cpp
This commit is contained in:
44
PAT/PAT-B/CPP/1079.延迟的回文数.cpp
Normal file
44
PAT/PAT-B/CPP/1079.延迟的回文数.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
bool isPal(string s){
|
||||
for(int i = 0; i < s.length() / 2; i++){
|
||||
if(s[i] != s[s.length() - i - 1])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
string add(string a, string b){
|
||||
string result;
|
||||
int num = 0, carry = 0;
|
||||
for(int i = 0; i < a.length(); i++){
|
||||
num = (a[i] - '0' + b[i] - '0') + carry;
|
||||
if(num >= 10){
|
||||
num -= 10;
|
||||
carry = 1;
|
||||
}else {
|
||||
carry = 0;
|
||||
}
|
||||
result += (num + '0');
|
||||
}
|
||||
if(carry == 1) result += '1';
|
||||
reverse(result.begin(), result.end());
|
||||
return result;
|
||||
}
|
||||
int main(){
|
||||
string a, b, c;
|
||||
cin >> a;
|
||||
int cnt = 1;
|
||||
while(!isPal(a) && cnt <= 10){
|
||||
cnt++;
|
||||
b = a;
|
||||
reverse(b.begin(), b.end());
|
||||
c = add(a, b);
|
||||
cout << a << " + " << b << " = " << c << endl;
|
||||
a = c;
|
||||
}
|
||||
if(cnt <= 10) cout << a << " is a palindromic number." << endl;
|
||||
else cout << "Not found in 10 iterations." << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user