杭电_ACM_Hat's Fibonacci
2012-11-05 10:40
232 查看
[align=left]Problem Description[/align] A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take a number as input, and print that Fibonacci number. |
[align=left]Input[/align] Each line will contain an integers. Process to end of file. |
[align=left]Output[/align] For each case, output the result in a line. |
[align=left]Sample Input[/align]100 |
[align=left]Sample Output[/align]4203968145672990846840663646 Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits. |
#include <stdio.h> #include <string.h> int f[10000][505] = {0}; int main() { int n, carry, sum, i, j, length; //f[number][0] is the length of the number f[1][0] = 1; f[1][1] = 1; f[2][0] = 1; f[2][1] = 1; f[3][0] = 1; f[3][1] = 1; f[4][0] = 1; f[4][1] = 1; while (scanf("%d", &n) != EOF) { if (n < 5) { puts("1"); continue; } for (i = 5; i <= n; i++) { f[i][0] = f[i - 1][0]; carry = 0; //splite the number by four for (j = 1; j <= f[i][0]; j++) { sum = f[i -1][j] + f[i - 2][j] + f[i - 3][j] + f[i - 4][j] + carry; f[i][j] = sum % 10000; carry = sum / 10000; } //if the carry is not zero, then the length must plus one if (carry != 0) { f[i][0]++; f[i][j] = carry; } } //formatted printing length = f [0]; printf("%d", f [length]); for (i = length - 1; i >= 1; i--) printf("%04d",f [i]); printf("\n"); } return 0; }
Main points
firstly, you can't use string in C and if the array is big, you must declare in global variable.
secondly, the question is splitting the number by four, or you will accure Runtime Error (ACCESS_VIOLATION). The one diffence with spliting by one is you should use %d firstly, then use %04d.
相关文章推荐
- 杭电acm专题四1009
- 杭电 ACM 1015 --Safecracker
- 【ACM】杭电1073:Online Judge
- 【ACM】杭电1141:Factstone Benchmark
- 杭电ACM 1089-1096
- 杭电acm ————1048
- 杭电ACM 1095 A+B for Input-Output Practice (VII)
- 杭电acm——1200
- 杭电acm 1520 Anniversary party(树形dp)
- 杭电ACM 2012 素数判定
- 杭电 ACM 几道有关string的题目
- 杭电 ACM 1.3.6
- 杭电acm 1060
- 杭电ACM 2021 发工资咯:)
- 杭电ACM 2028 Lowest Common Multiple Plus
- 杭电ACM题目分类
- 杭电ACM 1096 简单求和
- 杭电ACM 2035 人见人爱A^B
- 杭电acm 1018 Big Number
- 杭电ACM 2040 亲和数