#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
struct bign
{
char d[1005];//数组 不是字符串!!
int len;
bign()//初始化
{
memset(d,0,sizeof(d));//memset -1、0
len=0;
}
};
bign change(char s[])
{
bign tbign;
int len=strlen(s);
for(int i=0;i<len;i++)
{
tbign.d[i]=s[i]-'0';
}
tbign.len=len;
return tbign;
}
bign add(bign a,bign b)
{
bign sum;
reverse(a.d,a.d+a.len);
reverse(b.d,b.d+b.len);
int carry=0;
int i;
for(i=0;i<a.len||i<b.len;i++)
{
sum.d[i]=(a.d[i]+b.d[i]+carry)%10;
carry=(a.d[i]+b.d[i]+carry)/10;
}
if(carry!=0)
{
sum.d[i]=1;
sum.len=max(a.len,b.len)+1;
}
else
sum.len=max(a.len,b.len);
reverse(sum.d,sum.d+sum.len);
return sum;
}
void Pr(bign a)
{
for(int i=0;i<a.len;i++)
printf("%d",a.d[i]);
}
bool judge(bign a)
{
int len=a.len;
for(int i=0;i<a.len/2;i++)
{
if(a.d[i]!=a.d[len-i-1])
return 0;
}
return 1;
}
int main()
{
// freopen("in.txt","r" ,stdin);
char s[20];
int K;
scanf("%s %d",s,&K);
bign bn,bn1,bn2,sum;
bn=change(s);
if(judge(bn))
{
printf("%s\n",s);
printf("0");
return 0;
}
int i;
for(i=1;i<=K;i++)
{
bn1=bn;
bn2=bn;
reverse(bn2.d,bn2.d+bn2.len);
sum=add(bn1,bn2);
bn=sum;
if(judge(sum))
{
Pr(sum);
printf("\n%d",i);
break;
}
}
if(i==K+1)
{
Pr(sum);
printf("\n%d",K);
}
return 0;
}
1024. Palindromic Number (25)--大整数的结构体用法(构造函数初始化)及运算
最新推荐文章于 2022-07-02 21:28:00 发布