<一>兔子问题(来自谭浩强《C++程序设计》)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)//数列的前80项
{
cout<<setw(12)<<f1<<setw(12)<<f2;
if(i%2==0) cout<<endl;//每输完4个数换行,是每行输出个数为4
f1=f1+f2;//第三项为前两项的和
f2=f2+f1;//第四项是前两项的和
}
return 0;
}
<二>一只小蜜蜂...
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 93 Accepted Submission(s) : 25
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数。
其中,蜂房的结构如下所示。
其中,蜂房的结构如下所示。
Input
输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0<a<b<50)。
Output
对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行。
Sample Input
2 1 2 3 6
Sample Output
1 3
用的递归方法:
#include <stdio.h>
int main()
{
int i, j, n;
__int64 d[51] = {1, 1, 2,};
for (i = 3; i < 51; i++)
d[i] = d[i-1] + d[i-2];
scanf("%d", &n);
while (n-- && scanf("%d%d", &i, &j) != EOF)
printf("%I64d\n", i > j ? 0 : d[j-i]);
return 0;
}
<三>大菲波数
Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 36 Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
#include <iostream>
using namespace std;
int f[1001][2010];
int main()
{
int Pi;
int N;
cin>>N;
memset(f,0,sizeof(f)); //初始化:f是数组名,0是初始化的值,后面是全部初值化为0的意思
f[1][0]=1;
f[2][0]=1;
for(int i=3;i<1001;i++)
for(int j=0;j<2010;j++){
f[i][j]+=f[i-1][j]+f[i-2][j];
if(f[i][j]>=10)
{
f[i][j+1]=f[i][j]/10;
f[i][j]=f[i][j]%10;
}
}
while(N--)
{
cin>>Pi;
int flag=0;
for(int i=1009;i>=0;i--)
{
if(flag==0&&f[Pi][i]!=0) flag=1;
if(flag) cout<<f[Pi][i];
}
cout<<endl;
}
return 0;
}
<四>Hat's Fibonacci
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
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.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646
#include<iostream>
#include<cstring>
#define N 10000
#define M 300
using namespace std;
int f[N][M];
void work()
{
memset(f,0,sizeof(f));
f[1][1]=f[2][1]=f[3][1]=f[4][1]=1;
int i,j,t;
for(i=5;i<N;i++)
{
t=0;
for(j=1;j<M;j++)
{
t=t+f[i-1][j]+f[i-2][j]+f[i-3][j]+f[i-4][j];
f[i][j]=t%100000000;
t=t/100000000;
}
}
}
int main()
{
int n,i;
work();
while(scanf("%d",&n)!=EOF)
{
i=M-1;
while(f[n][i]==0)i--;
printf("%d",f[n][i--]);
while(i>=1)
{
printf("%08d",f[n][i]);
i--;
}
printf("\n");
}
return 0;
}