UVA10579 Fibonacci Numbers【大数】
A Fibonacci sequence is calculated by adding the previous two members of the sequence,with the first two members being both 1. f(1) = 1,f(2) = 1,f(n > 2) = f(n ? 1) + f(n ? 2) Input Your task is to take numbers as input (one per line). Output And then print the corresponding Fibonacci number (one per line also). Sample Input 3 100 Sample Output 2 354224848179261915075 Note: No generated fibonacci number in excess of 1000 digits will be in the test data,i.e. f(20) = 6765 has 4 digits. 问题链接:UVA10579 Fibonacci Numbers 问题简述:(略) 问题分析:一个简单的大数加法问题。 程序说明: ? 用数组来模拟大数计算。这里用二维数组来进行模拟计算Fibonacci数列。 ? 打表,根据输入的n,输出结果。因为不超过1000位,所以打表到fib(5000),根据Fibonacci数列的通项公式,下式是成立的: 1000 < lg(Fib(5000))<= lg(0.5(sqrt(5)+ 1)^ 5000 )<= 1045 题记:(略) 参考链接:(略) AC的C++语言程序如下: /* UVA10579 Fibonacci Numbers */ #include <bits/stdc++.h> using namespace std; const int N = 5000; const int N2 = 1000; char fib[N + 1][N2 + 1]; int main() { memset(fib,sizeof(fib)); fib[1][0] = fib[2][0] = 1; for(int i = 3; i <= N; i++) { for(int j = 0; j <= N2; j++) fib[i][j] = fib[i - 2][j] + fib[i - 1][j]; for(int j = 0; j<= N2; j++) { fib[i][j + 1] += fib[i][j] / 10; fib[i][j] %= 10; } } int n; while(~scanf("%d",&n)) { int right = N2; while(right > 0 && !fib[n][right]) right--; while(right >=0) printf("%d",fib[n][right--]); printf("n"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |