大菲波数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21198 Accepted Submission(s): 7163
Problem Description
Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。
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
5 1 2 3 4 5
Sample Output
1 1 2 3 5
Source
问题链接:HDU1715 大菲波数。
问题简述:参见上文。
问题分析:大数问题。为了避免重复计算,需要打表。
程序说明:(略)
/* HDU1715 大菲波数 */
#include <iostream>
#include <string>
using namespace std;
const int MAXN = 1000;
string s[MAXN+1];
void maketable(int n)
{
string a, b;
s[1] = "1";
s[2] = "1";
for(int i=3; i<=n; i++) {
a = "00" + s[i-2];
b = "0" + s[i-1];
int val, carry=0;
for(int j=(int)a.length()-1, k=(int)b.length()-1; k>=0; j--, k--) {
val = (a[j] - '0') + (b[k] - '0') + carry;
carry = val / 10;
b[k] = val % 10 + '0';
}
if(b[0] == '0')
b.erase(0, 1);
s[i] = b;
}
}
int main()
{
int n, pi;
maketable(MAXN);
cin >> n;
while(n--) {
cin >> pi;
cout << s[pi] << endl;
}
return 0;
}