- 论坛徽章:
- 0
|
如果我没有理解错的话,是这样的吧
第100年为4563
#include <iostream>
#include <cstdio>
#include <cassert>
#define GROWING_STEP 4 //每头小牛需要多长时间成为母牛(可以生新的小牛)
using namespace std;
int main(int argc, char *argv[])
{
int total;
int step; //control the increment cow number
if (argc!=2)
{
cout<<"wrong parameter"<<endl;
exit(-1);
}
int years = atoi(argv[1]);
assert (years > 0);
if (years < GROWING_STEP) //当输入年数小于4,只有一头牛,因为初始的小牛还没有成熟
{
total = 1;
}
else if(years < (GROWING_STEP*2-1)) //输入年数小于7,只有一头牛生产,此时增量固定为1.
{
total =1;
step = 1;
for (int i= GROWING_STEP; i<=years; i++)
{
total += step;
}
}
else if(years >= (GROWING_STEP*2-1)) //从第7年开始,此时每年都会有新的小牛成为母牛.所以增量为1,2,3,4,5自然数列.
{
total = GROWING_STEP;
step = 1;
for (int i=GROWING_STEP*2-1; i<=years; i++)
{
step++;
total+=step;
}
}
else; //空语句,无用,仅仅为了格式
cout<<"after "<<years<<" years, the number of cows are :"<<total<<endl;
return 0;
} |
|