15
回答
关于母牛的算法题求解答
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
题目为:有一母牛,到4岁可生育,每年一头,所生均是一样的母牛,到15岁绝育,不再能生,20岁死亡,问n年后有多少头牛?
<无标签>
举报
yangzizhe
发帖于6年前 15回/1K+阅
共有15个答案 最后回答: 5年前

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

用最基本的方法,创建一个有20个整数的数组,分别存储1至20岁的母牛。

假设第一头母牛刚出生。

#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;

	printf("Please input the years:");
	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;
}

Please input the years:20

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;

	printf("Please input the years:");
	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跟我的答案一样,应该是正确的 5年前 回复
张金富正不正确我也不知道,毕竟题目本身很模糊,明白原理即可。 6年前 回复

引用来自“Grrrr”的答案

植物生长,母牛下崽,兔子下崽,爬楼梯. 统统都是斐波那契数列.

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

建议给个答案吧!斐波那契数列懂的,但是这个题可能不仅仅斐波那契数列那么简单,究竟应该怎么解呢?网上也有答案,可惜我没有找到正确的。

都是斐波那契数列类似的问题

例如 第一年 1 第二年 1

我写出前9年的数量:1,1,1,2,3,4,6,9,13

可以知道第n年是n-1年与n-3年的结果的和。初始为1,1,1

到了15岁不能生了就是 f(n-1)+f(n-3)-f(n-14)

到了20岁就挂掉就是 f(n-1)+f(n-3)-f(n-14)-f(n-19)

写了一段简单的程序, 不知道对不.

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;
}

用最基本的方法,创建一个有20个整数的数组,分别存储1至20岁的母牛。

假设第一头母牛刚出生。

#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;

	printf("Please input the years:");
	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;
}

Please input the years:20

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 条评论 ---
张金富给个最佳答案吧! 6年前 回复
顶部