標籤:hdu1715
大菲波數Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11137 Accepted Submission(s): 3769
Problem DescriptionFibonacci數列,定義如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
計算第n項Fibonacci數值。
Input輸入第一行為一個整數N,接下來N行為整數Pi(1<=Pi<=1000)。
Output輸出為N行,每行為對應的f(Pi)。
Sample Input
512345
Sample Output
11235
#include <stdio.h>#include <string.h>char dp[1002][1000];char buf[1000];void cal(char str1[], char str2[], char str[]){int len1 = strlen(str1), id = 0;int len2 = strlen(str2), i;memset(buf, 0, sizeof(buf));while(len1 > 0 && len2 > 0)buf[id++] = str1[--len1] - '0' + str2[--len2] - '0';while(len1 > 0) buf[id++] = str1[--len1] - '0';while(len2 > 0) buf[id++] = str2[--len2] - '0';for(i = 0; i <= id; ++i)if(buf[i] > 9){++buf[i+1];buf[i] -= 10;}if(!buf[id]) --id;i = 0;while(id >= 0) str[i++] = buf[id--] + '0';str[i] = '\0';}int main(){dp[1][0] = dp[2][0] = '1';dp[1][1] = dp[2][1] = '\0';int i, n;for(i = 3; i <= 1000; ++i)cal(dp[i-1], dp[i-2], dp[i]);scanf("%d", &n);while(n--){scanf("%d", &i);printf("%s\n", dp[i]);}return 0;}