A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤10e10) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.
Output Specification:
For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.
Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3
Note1
- 思路清晰明了,stl手到擒来, 代码一气呵成,跑过样例愉快提交,结果真香。。
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
string s;
bool Check(void){
int p = 0, q = s.size() - 1;
while(p <= q){
if(s[p] != s[q]) return 0;
p++;
q--;
}
return 1;
}
void process(void){
string s1 = s;
reverse(s.begin(), s.end());
long long sum = stoi(s) + stoi(s1);
s = to_string(sum);
}
int main(){
int step, cnt= 0;
cin >> s >> step;
if(Check()) cout << s << endl << 0 ;
else{
while(cnt < step){
cnt++;
process();
if(Check()){
cout << s << endl << cnt ;
return 0;
}
}
}
cout << s << endl << cnt;
return 0;
}
Note2
- 运行时错误,具体不知,大数已经超过long long 范围,不能利用 stoi相加
- 大数相加模板, 划重点
- 因为本身是回文的输出完没return 还以为是while 和for的逻辑错误,查了半天。
Code2
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
string s;
bool Check(void){
string s1 = s;
reverse(s1.begin(), s1.end());
if(s == s1) return 1;
else return 0;
}
void process(string t) {
int len = s.length(), carry = 0;
for(int i = 0; i < len; i++) {
s[i] = s[i] + t[i] + carry - '0';
carry = 0;
if(s[i] > '9') {
s[i] = s[i] - 10;
carry = 1;
}
}
if(carry) s += '1'; //末尾拼接1, 其实就是没对称的最高位
reverse(s.begin(), s.end());
}
int main(){
int step, cnt= 0;
cin >> s >> step;
// if(Check()) cout << s << endl << 0 ;
// else{
for(cnt = 0; cnt < step; cnt++){
string t = s;
reverse(t.begin(), t.end());
if(Check()){
break;
}
process(t);
}
// }
cout << s << endl << cnt;
return 0;
}
Note3
总结大数相加的程序
Code3
string add(string s1, string s2) {
int len1 = s1.size(),len2 = s2.size(), i,j , carry = 0;
string s3(abs(len1 - len2), '0'); //较短的字符串前面补‘0’,使两个字符串等长。
string sum = (len1 > len2) ? s1 : s2;
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
if(len1 > len 2) s2 += s3;
else if(len1 < len2) s1 += s3;
for( i = 0; i < len1 || i < len2; i++) {
sum[i] = s1[i] + s2[i] + carry - '0';
carry = 0;
if(sum[i] > '9') {
sum[i] = sum[i] - 10;
carry = 1;
}
}
if(carry) sum += '1'; //末尾拼接1, 其实就是没对称的最高位
reverse(sum.begin(), sum.end());
return sum;
}
Note4
大数相减的程序
string Minus(string s1, string s2){
int len1 = s1.size(), len2 = s2.size(), i, j, borrow = 0, flag = 0;
if(len2 > len1){
flag = 1;
string temp = s1;
s1 =s2;
s2 = temp;
}
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
string res = s1, makeup(abs(len1 - len2) , '0');
s2 += makeup;
for(i = 0; i < len1; i++){
if(s1[i] + borrow >= s2[i]){
res[i] = s1[i] + borrow - s2[i] + '0';
borrow = 0;
}
else{
res[i] = (10 - (s2[i] - '0')) + s1[i] + borrow;
borrow = -1;
}
}
if(flag) res += '-';
reverse(res.begin(), res.end());
return res;
}