15

<无标签>

yangzizhe

#### 引用来自“张金富”的答案

```#include <stdio.h>

int main(void) {
int n[20];
int year;
int i, j, sum = 0;
n[0] = 1;
for (i = 1; i < 20; i++)
n[i] = 0;

fflush(stdout);
scanf("%d", &year);

for (i = 0; i < year; i++) {
sum = 0;
for (j = 0; j < 20; j++) {
//printf("n[%d]=%d;", j, n[j]);
sum += n[j];
}
printf("After %d years there are %d cows\n", i, sum);
for (j = 19; j > 0; j--) {
n[j] = n[j - 1];
}
n[0] = 0;
for (j = 3; j < 15; j++) {
n[0] += n[j];
}

}

return 0;
}```

After 0 years there are 1 cows

After 1 years there are 1 cows

After 2 years there are 1 cows

After 3 years there are 2 cows

After 4 years there are 3 cows

After 5 years there are 4 cows

After 6 years there are 6 cows

After 7 years there are 9 cows

After 8 years there are 13 cows

After 9 years there are 19 cows

After 10 years there are 28 cows

After 11 years there are 41 cows

After 12 years there are 60 cows

After 13 years there are 88 cows

After 14 years there are 129 cows

After 15 years there are 188 cows

After 16 years there are 275 cows

After 17 years there are 403 cows

After 18 years there are 589 cows

After 19 years there are 861 cows

```#include <stdio.h>

/*n[0]代表新生的小牛
*n[1]代表1岁的小牛
*n[i]代表i岁的小牛
*n[19]代表19的岁的小牛
*sum代表总的小牛数
*新生的小牛四年后，也就是四岁了才可以生小牛。
*/
int main(void) {
int n[20];
int year;
int i, j, sum = 0;
n[0] = 1;
for (i = 1; i < 20; i++)
n[i] = 0;

fflush(stdout);
scanf("%d", &year);

for (i = 1; i < year; i++) {
sum = 0;

/*初始化当年1-19岁不同年龄段小牛的总数；*/
for (j = 19; j > 0; j--) {
n[j] = n[j - 1];
}

/*初始化当年新生的小牛的总数 */
n[0] = 0;
for (j = 4; j < 15; j++) {
n[0] += n[j];
}

/*计算当年过后牛的总数*/
for (j = 0; j < 20; j++) {
//printf("n[%d]=%d;", j, n[j]);
sum += n[j];
}
printf("After %d years there are %d cows\n", i, sum);
}

return 0;
}```

--- 共有 2 条评论 ---
zengnjin跟我的答案一样，应该是正确的 6年前

#### 引用来自“Grrrr”的答案

f(x) = f(x-1) + f(x-2)

```int fun(int year);

int main(int argc, char** argv)
{
int sum = 0;
int year = atoi(argv[1]);

sum = fun(year) + 1; /* +1 第一头母牛. */
printf("%d years, sum: %d\n", year, sum);

return 0;
}

int fun(int year)
{
int age = 0;
int sum = 0;
int i = 0;

for (i = year; i >= 1; i--)
{
age++;
if (age >= 4 && age < 15) /* < 15 15岁不下崽. */
sum += fun(i) + 1;
}
if (age >= 20) /* -1 死牛. */
sum -= 1;

return sum;
}```

```#include <stdio.h>

int main(void) {
int n[20];
int year;
int i, j, sum = 0;
n[0] = 1;
for (i = 1; i < 20; i++)
n[i] = 0;

fflush(stdout);
scanf("%d", &year);

for (i = 0; i < year; i++) {
sum = 0;
for (j = 0; j < 20; j++) {
//printf("n[%d]=%d;", j, n[j]);
sum += n[j];
}
printf("After %d years there are %d cows\n", i, sum);
for (j = 19; j > 0; j--) {
n[j] = n[j - 1];
}
n[0] = 0;
for (j = 3; j < 15; j++) {
n[0] += n[j];
}

}

return 0;
}```

After 0 years there are 1 cows

After 1 years there are 1 cows

After 2 years there are 1 cows

After 3 years there are 2 cows

After 4 years there are 3 cows

After 5 years there are 4 cows

After 6 years there are 6 cows

After 7 years there are 9 cows

After 8 years there are 13 cows

After 9 years there are 19 cows

After 10 years there are 28 cows

After 11 years there are 41 cows

After 12 years there are 60 cows

After 13 years there are 88 cows

After 14 years there are 129 cows

After 15 years there are 188 cows

After 16 years there are 275 cows

After 17 years there are 403 cows

After 18 years there are 589 cows

After 19 years there are 861 cows

--- 共有 1 条评论 ---