C++编程题!(一道牛题!高手进!!)急,答对有追加分数!!!

来源:百度知道 编辑:UC知道 时间:2024/07/06 20:14:08
有一头母牛,从第四年当年开始每一年生一头母牛(所有牛只生母牛),生下来的牛从出生第四年开始也每年“生且只生”一头母牛,依此类推。每头牛的寿命都是三十年(第三十一年死掉,生不了了),问第N年有多少头牛?要求:用户输入年数,程序输出牛的数目。

/*随便写了一个,很久没写了,可能有语法错误*/

#include "stdio.h"
#include "conio.h"

main()
{
int i = 0;/*年数*/
int n = 1;/*牛*/
int j = 0;/*当前年数*/
int k = 0;/*死牛导致的不该存在的牛*/

printf("How many years?\n");
scanf("%d",&i);/*取得年数*/

/*不死牛的情况*/
for(;i-j〉=4;j=j+4){
n = n * 2;
}

/*死牛的影响*/
for(j=i-31;j>=0;j=j-4){
if(k==o){
k = 1;
}
else{
k=k*2;
}
}

n = n-k;
printf("%d年后的牛:%d",i,n);
}

#include <iostream>
#include <stdio.h>
#include <vector>

void FunctionThree( int n )
{
using namespace std;
vector<int> iVecYears;
vector<int>::iterator it;
iVecYears.push_back(0);
int cnt=0, yearCnt=0