题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1024
数字转换,大数加法
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void Add_string(string &a, string &b)
{
int i=a.size()-1;
int flag=0;
while(i>=0)
{
a[i] += b[i]-'0'+flag;
flag = 0;
if(a[i] > '9')
{
flag =1;
a[i] -= 10;
}
i--;
}
if(flag == 1)
{
a.insert(a.begin(), 1, '1');
}
}
int main()
{
string str;
int k;
cin >> str >> k;
int step=0;
while(step<k)
{
string shadow = str;
reverse(shadow.begin(), shadow.end());
if(shadow == str)
{
break;
}
else
{
Add_string(str, shadow);
step++;
}
}
cout << str << endl;
cout << step << endl;
return 0;
}