Description
数列A满足An = An-1 + An-2 + An-3, n >= 3
编写程序,给定A0, A1 和 A2, 计算A99
Input
输入包含多行数据
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
Output
对于输入的每一行输出A99的值
Sample Input
1 1 1
Sample Output
69087442470169316923566147
看到样例就应该知道是一个大数模拟。想起之前cf一道题上,我还傻乎乎的用了大数模拟,其实long long完全装得下。一看这么多位数,吓得我开了4个long long,然而有的时候后几位开头可能是0,因此需要用printf(“%.10I64d”,a[4]);的姿势。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
long long a[4], b[4], c[4], d[4], temp, pre;
int main()
{
int i, j, m, n, ans, a0, a1, a2;
temp = 1;
for (i = 1; i <= 10; i++)
temp = temp * 10;
while (scanf("%d%d%d", &a0, &a1, &a2) != EOF)
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(d, 0, sizeof(d));
d[0] = a0; d[1] = a1; d[2] = a2;
for (i = 3; i <= 99; i++)
{
d[3] = d[0] + d[1] + d[2];
pre = 0;
if (d[3] >= temp)
{
pre = d[3] / temp;
d[3] = d[3] - pre* temp;
}
c[3] = c[0] + c[1] + c[2] + pre;
pre = 0;
if (c[3] >= temp)
{
pre = c[3] / temp;
c[3] = c[3] - pre* temp;
}
b[3] = b[1] + b[2] + b[0] + pre;
pre = 0;
if (b[3] >= temp)
{
pre = b[3] / temp;
b[3] = b[3] - pre* temp;
}
a[3] = a[0] + a[1] + a[2] + pre;
a[0] = a[1]; b[0] = b[1]; c[0] = c[1]; d[0] = d[1];
a[1] = a[2]; b[1] = b[2]; c[1] = c[2]; d[1] = d[2];
a[2] = a[3]; b[2] = b[3]; c[2] = c[3]; d[2] = d[3];
}
if (a[3])
{
cout << a[3];
printf("%.10I64d", b[3]);
printf("%.10I64d", c[3]);
printf("%.10I64d", d[3]);
cout << endl;
}
else if (b[3])
{
cout << b[3];
printf("%.10I64d", c[3]);
printf("%.10I64d", d[3]);
cout << endl;
}
else if (c[3])
{
cout << c[3];
printf("%.10I64d", d[3]);
cout << endl;
}
else
cout << d[3] << endl;
}
return 0;
}
看到学长的java简单的通过,心中十万头草泥马……