序列
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6614 | Accepted: 2951 |
Description
数列A满足An = An-1 + An-2 + An-3, n >= 3
编写程序,给定A0, A1 和 A2, 计算A99
编写程序,给定A0, A1 和 A2, 计算A99
Input
输入包含多行数据
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
Output
对于输入的每一行输出A99的值
Sample Input
1 1 1
Sample Output
69087442470169316923566147
#include <stdio.h> int a[1000]={0}, b[1000]={0}, c[1000]={0}; void GetSum(int d, int e, int f) { int i = 0, jinwei = 0, x, digit = 0, j, k,temp; int *sum; int *left; int *right; while(d) { a[i] = d % 10; d /= 10; i++; } digit = i; i = 0; while(e) { b[i] = e % 10; e /= 10; i++; } if(i>digit) { digit = i; } i = 0; while(f) { c[i] = f % 10; f /= 10; digit++; i++; } if(i>digit) { digit = i; } sum = a; left = b; right = c; for(i = 3; i < 100; i++) { for(j = 0; j < digit || jinwei >0; j++) { x = left[j]+right[j]+sum[j]+jinwei; jinwei = x/10; sum[j] = x%10; } digit = j; if(i==99) break; if(sum==c){sum=a;left=b;right=c;} else if(sum==a){sum=b;left=c;right=a;} else if(sum==b){sum=c;left=a;right=b;} } for(k = j-1; k >=0; k--) printf("%d",sum[k]); printf("\n"); } int main() { int d,e,f; while(scanf("%d %d %d", &d, &e, &f) != EOF) { if(d==0 && e==0 && f==0) printf("0\n"); else GetSum(d,e,f); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); } system("pause"); return 0; }
测试数据: 0 0 0 0 1 1 1 69087442470169316923566147 2 2 2 138174884940338633847132294 3 3 3 207262327410507950770698441 4 4 4 276349769880677267694264588 5 6 7 427754062987361451295487810 9 9 9 621786982231523852312095323 111 111 111 7668706114188794178515842317 234 455 566 31169355880763566758453906103 0 1 0 24332675219681431451788241 0 0 1 28992087708416717612934417 1 1 0 40095354761752599310631730感谢在讨论区分享出数据的雷锋!