吃糖果
现有n(20 > n > 0)个糖果,每天可以吃1个,也可以每天吃2个,也可以每天吃3个,请计算共有多少种不同的吃法。
时间限制:1000
内存限制:65536
输入
输入的每一行包括一组测试数据,即为糖果数n。最后一行为0,表示测试结束。
输出
每一行输出对应一行输入的结果,即为吃法的数目。
样例输入
1
2
3
4
0
样例输出
1
2
4
7
本题为递归,类似兔子数列
#include<bits/stdc++.h>
using namespace std;
int n;
int s(int n){
if(n==0)return 1;
if(n==1)return 1;
if(n==2)return 2;
else return s(n-1)+s(n-2)+s(n-3);
}
int main(){
cin>>n;
while(n){
cout<<s(n)<<endl;
cin>>n;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int s(int N)
{
if(N==0)
return 1;
if(N==1)
return 1;
if(N==2)
return 2;
else
return s(N-1)+s(N-2)+s(N-3);
}
int main()
{
int N;
cin>>N;
while(N)
{
cout<<s(N);
cin>>N;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
while (1) {
cin >> n;
if (n == 0) {
break;
}
int i = 1, j = 2, k = 4;
if (n == 1) {
cout << 1 << endl;
} else if (n == 2) {
cout << 2 << endl;
} else if (n == 3) {
cout << 4 << endl;
} else if (n > 3) {
int v;
for (int p = 4; p <= n; ++p) {
v = i + j + k;
i = j;
j = k;
k = v;
}
cout << v << endl;
}
}
return 0;
}