马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 798236606 于 2020-2-1 11:32 编辑
传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805476473028608
解:
大整数运算#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct BIGN{
int num[1000];
int len;
bool is_neg;
BIGN()
{
memset(num, 0, sizeof(num));
len = 0;
is_neg = false;
};
}bign;
bign str2bign(char *s)
{
bign a;
a.len = strlen(s);
if (s[0] == '-')
{
a.is_neg = true;
a.len--;
s++;
}
for (int i = 0; i < a.len; i++)
a.num[i] = s[a.len - 1 - i] - '0';
return a;
}
void print_bign(bign a)
{
if (a.is_neg)
putchar('-');
for (int i = a.len - 1; i >= 0; i--)
printf("%d", a.num[i]);
}
bign u_add(bign a, bign b)
{
bign c;
int carry = 0;
for (int i = 0; i < a.len || i < b.len; i++)
{
int temp = a.num[i] + b.num[i] + carry;
c.num[c.len++] = temp % 10;
carry = temp / 10;
}
if (carry)
c.num[c.len++] = carry;
return c;
}
bool is_pal(bign a)
{
for (int i = 0; i <= a.len / 2; i++)
if (a.num[i] != a.num[a.len - 1 - i])
return false;
return true;
}
bign make_pal(bign a, int &k)
{
int lim = k;
for (k = 0; k < lim && !is_pal(a); k++)
{
bign b = a;
reverse(b.num, b.num + b.len);
a = u_add(a, b);
}
return a;
}
int main(void)
{
char s[15];
int k;
scanf("%s %d", s, &k);
print_bign(make_pal(str2bign(s), k));
printf("\n%d", k);
}
|