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 (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.
Output Specification:
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 (<= 1010) 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.
IDEA
1.用到了string的翻转函数reverse()
2.int, long long都会溢出,考查string类型大数加法
3.新学到一个string的插入函数insert(str.begin, c)//每次都在str的开头插入一个字符
CODE
#include<iostream>
#include<algorithm>
#include<sstream>
#include<cstring>
#include<cmath>
using namespace std;
int isPalindromic(string str){
for(int i=0,j=str.length()-1;i<str.length()/2;i++,j--){
if(str[i]!=str[j]){
return 0;
}
}
return 1;
}
string strPlus(string a,string b){
string result="";
int len_a=a.length(),len_b=b.length();
int sum,flag=0;//flag=1表示进位
for(int i=len_a-1,j=len_b-1;i>=0||j>=0;i--,j--){
if(i<0){
sum=b[j]-'0';
}else if(j<0){
sum=a[i]-'0';
}else{
sum=a[i]-'0'+b[j]-'0';
}
sum+=flag;
result.insert(result.begin(),sum%10+'0'); //在begin出插入字符,即每次都在头部插入字符
flag=sum/10;
}
if(flag){
result.insert(result.begin(),flag+'0');
}
return result;
}
int main(){
string n;
int k;
cin>>n>>k;
int count=0;
string tmp=n;
while(count<=k){
if(isPalindromic(tmp)){
cout<<tmp<<endl<<count;
break;
}
if(count==k){
cout<<tmp<<endl<<k;
break;
}
string tmp1=tmp;
reverse(tmp1.begin(),tmp1.end());
tmp=strPlus(tmp,tmp1);
count++;
}
return 0;
}