c 母牛的故事

Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input
2
4
5
0

Sample Output
2
4
6

代码如下:
#include<stdio.h>
#include <math.h>
int main()
{
int y1=0,y2=0,y3=0,big=1,i,n;
while(scanf("%d",&n)!=EOF)
{
if(n==0){
continue;
}
for(i=1;i<n;i++){
if(y3>0){
big+=y3;
y3 = 0;
}
if(y2>0){
y3+=y2;
y2 = 0;
}
if(y1>0){
y2+=y1;
y1 = 0;
}
if(big>0){
y1+=big;
}
}
printf("%d\n",y1+y2+y3+big);
y1=0;y2=0;y3=0;big=1;//reset

}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40811682/article/details/88016508