大菲波数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 2947
Problem Description
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
Recommend
lcy
思路:我弱爆了,此等水题居然花了我一小时
好坑啊,打水坑
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
char sum[1010][1000];
int hash[1010];
int mmm;
int n,t;
int len1,len2;
int main()
{
memset(sum,0,sizeof(sum));
memset(hash,0,sizeof(hash));
sum[1][0] = 1;
hash[1] = 1;
sum[2][0] = 1;
hash[2] = 1;
sum[3][0] = 1;
hash[3] = 1;
sum[4][0] = 3;hash[4] = 1;
sum[5][0] = 5;hash[5] = 1;
sum[6][0] = 8;hash[6] = 1;
sum[7][0] = 3;sum[7][1] = 1;hash[7] = 2;
sum[8][0] = 1;sum[8][1] = 2;hash[8] = 2;
for(int i = 3;i <=1000;i ++)
{
int biji = 0;
int j;
for(j = 0;j < hash[i - 1];j ++)
{
mmm = sum[i - 1][j] + sum[i - 2][j] + biji;
sum[i][j] = mmm % 10;
biji = mmm / 10;
}
hash[i] = hash[i - 1];
j --;//细节决定成败啊
while(biji > 9)
{
j ++;
hash[i] ++;
sum[i][j] = biji % 10;
biji = biji / 10;
}
if(biji)
{
j ++;
hash[i] ++;
sum[i][j] = biji;
}
}
scanf("%d",&t);
while(t --)
{
scanf("%d",&n);
for(int h = hash[n] - 1;h >= 0;h --)
printf("%d",sum[n][h]);
printf(" ");
}
}
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
char sum[1010][1000];
int hash[1010];
int mmm;
int n,t;
int len1,len2;
int main()
{
memset(sum,0,sizeof(sum));
memset(hash,0,sizeof(hash));
sum[1][0] = 1;
hash[1] = 1;
sum[2][0] = 1;
hash[2] = 1;
sum[3][0] = 1;
hash[3] = 1;
sum[4][0] = 3;hash[4] = 1;
sum[5][0] = 5;hash[5] = 1;
sum[6][0] = 8;hash[6] = 1;
sum[7][0] = 3;sum[7][1] = 1;hash[7] = 2;
sum[8][0] = 1;sum[8][1] = 2;hash[8] = 2;
for(int i = 3;i <=1000;i ++)
{
int biji = 0;
int j;
for(j = 0;j < hash[i - 1];j ++)
{
mmm = sum[i - 1][j] + sum[i - 2][j] + biji;
sum[i][j] = mmm % 10;
biji = mmm / 10;
}
hash[i] = hash[i - 1];
j --;//细节决定成败啊
while(biji > 9)
{
j ++;
hash[i] ++;
sum[i][j] = biji % 10;
biji = biji / 10;
}
if(biji)
{
j ++;
hash[i] ++;
sum[i][j] = biji;
}
}
scanf("%d",&t);
while(t --)
{
scanf("%d",&n);
for(int h = hash[n] - 1;h >= 0;h --)
printf("%d",sum[n][h]);
printf(" ");
}
}