简单递归
放苹果
把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。Input
Output
Sample Input
1 7 3
Sample Output
8
#include<iostream> #include<cstring> using namespace std; int a[21][21]; int apple(int m,int n) { if(a[m][n])return a[m][n]; else if(m==1 || n==1) { a[m][n]=1; return a[m][n]; } else if(m<n) { a[m][n]=apple(m,m); return a[m][n]; } else if(m==n) { a[m][n]=1+apple(m,m-1); return a[m][n]; } else { a[m][n]=apple(m-n,n)+apple(m,n-1); return a[m][n]; } } int main() { int T,m,n; cin>>T; while(T--) { memset(a,0,sizeof(a)); cin>>m>>n; cout<<apple(m,n)<<endl; } return 0; }
母牛的故事
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?Input输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。Output对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。Sample Input
2 4 5 0
Sample Output
2 4 6
#include<iostream> using namespace std; long long a[100]; int main() { int n; while(cin>>n) { a[1]=1;a[2]=2;a[3]=3;a[4]=4; if(n==0) break; if(n<=4) cout<<n<<endl; else { for(int i=5;i<=n;i++) { a[i]=a[i-1]+a[i-3]; } cout<<a[n]<<endl; } } return 0; }
Fibonacci Again
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
OutputPrint the word "yes" if 3 divide evenly into F(n).
Print the word "no" if not.
Sample Input
0 1 2 3 4 5
Sample Output
no no yes no no no
#include<iostream> using namespace std; int a[10000004]; int main() { int n; a[0]=1; a[1]=2; for(int i=2;i<=1000000;i++) { a[i]=(a[i-1]+a[i-2])%3; } while(cin>>n) { if(a[n]==0) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }
Children’s Queue
There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?
InputThere are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)OutputFor each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.Sample Input
1 2 3
Sample Output
1 2 4
#include<iostream> #include<cstring> using namespace std; const int maxn=1100; int a[maxn][maxn]; int main() { int n,i,j; memset(a,0,sizeof(a)); a[0][0]=0; a[1][0]=1; a[2][0]=2; a[3][0]=4; a[4][0]=7; for(i=5;i<=1001;i++) { for(j=0;j<=1007;j++) { a[i][j]+=(a[i-1][j]+a[i-2][j]+a[i-4][j]); if(a[i][j]>=10) { int h=a[i][j]; a[i][j]=h%10; a[i][j+1]+=(h/10); } } } while(cin>>n) { for(i=1007;i>=0;i--) { if(a[n][i]) break; } for( ;i>=0;i--) { cout<<a[n][i]; } cout<<endl; } return 0;
}
一只小蜜蜂...
有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房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<iostream> using namespace std; __int64 a[52]; int main() { __int64 N,c,b,i; a[0]=1; a[1]=1; for(i=2;i<=50;i++) { a[i]=a[i-1]+a[i-2]; } cin>>N; while(N--) { cin>>c>>b; cout<<a[b-c]<<endl; } return 0; }