解题思路:

        第n年的母牛的来源分别来自于前一年剩下的和往前推3年的母牛(能生的母牛,最早的也算在里面)

       

第一年
第二年
第三年
第四年
第五年
第六年
1
2
3
4

4(第四年)+2(第二年)=6

f(5-1) + f(5-3) = 6

f(4) + f(2) = 6

6(第五年)+3(第三年)=9

f(6-1) + f(6-3) = 9

f(5) + f(3) = 9

    

        6 = 前一年剩下的4头牛+(第2年新产的母牛刚具有生育能力生下的1头+最早的母牛1头)

        解题公式:f(n) = f(n-1) + f(n-3)

注意事项:


解法1 递归

import java.util.Scanner;

public class Main{

    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        while(true){
            int n = sc.nextInt();
            if(n==0)break;
            System.out.println(f(n));
        }
        
    }
    
    public static int f(int n){
        //前四天没有母牛出生
        if(n<=4){
            return n;
        }
        //上一天出生数加前三天的出生数
        return f(n-1)+f(n-3);
    }
    
}

解法2 记忆型递归+剪枝  避免计算重复子问题

import java.util.Scanner;

public class Main{

    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        while(true){
            int n = sc.nextInt();
            if(n==0)break;
            System.out.println(f(n));
        }
        
    }
    
    public static Integer[] arr = new Integer[999];
    
    public static int f(int n){ 
        //前四天没有母牛出生       
        if(n<=4){
            arr[n]=n;
            return n;
        }
        //剪枝避免计算重复子问题
        if(arr[n]!=null){
            return arr[n];
        }
        //记住存储结果 以便复用
        arr[n]=f(n-1)+f(n-3);        
        return arr[n];
    }
}

解法3 动态规划

import java.util.Scanner;

public class Main{

    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        int [] arr = new int[999];
        while(true){
            int n = sc.nextInt();
            if(n==0)break;
            for(int i = 1;i <= n;i++ ){
                //前四天没有母牛出生    
                if(i<=4){
                    arr[i]=i;
                }else{
                    arr[i]=arr[i-1]+arr[i-3];
                }
            }
            System.out.println(arr[n]);
        }
        
    }
    
}


点赞(0)
 

0.0分

91 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 34 条评论

keheia 1年前 回复TA
#include <stdio.h>
int main() {
	void mn(int n);
	int p;
	while(1){
		scanf("%d",&p);
		if(p!=0){
			mn(p);
		}
		else
			break;
	}
	return 0;
	
}
void mn(int n){
	int x[3]={1,0,0} ;
	int a,b,c; 
	a=n;
	for(b=1;b<=a-1;b++){
		c=x[2];	
		x[2]=x[1];
		x[1]=x[0];
		x[0]=x[0]+c;
	}
	printf("%d\n",x[0]+x[1]+x[2]);
	//printf("%d%d%d\n",x[0],x[1],x[2]);	
	return 0;
}
hyf 1年前 回复TA
#include<stdio.h>
int fun(int);
int main()
{
    int n;
    while(scanf("%d",&n) && n)
    {
        printf("%d",fun(n));
    }
    return 0;
}
int fun(int n)
{
    if(n < 4) return n;
    if(n >= 4)
    {
        return fun(n - 1) + n - 3;
    }
}
搞不懂了明明测试结果都一样,结果运行时间太长
挖掘机师傅 2年前 回复TA
感谢大佬分享
uq_20571766213 2年前 回复TA
#include <stdio.h>

int count_cow (int n)
{
    if (n == 1)
    return 1;
    
    if (n == 2)
    return 2;
    
    if (n == 3) 
    return 3;
    
    if (n == 4)
    return 4;
    
    return count_cow(n - 1) + count_cow(n - 3);
}

int main()
{
    int years;
    while(scanf("%d", &years) && years !=0)
    {
        printf("%d \n", count_cow(years));
    }
    return 0;
}
uq_20571766213 2年前 回复TA
#include <stdio.h>

int count_cow (int n)
{
    if (n == 1)
    return 1;
    
    if (n == 2)
    return 2;
    
    if (n == 3) 
    return 3;
    
    if (n == 4)
    return 4;
    
    return count_cow(n - 1) + count_cow(n - 3);
}

int main()
{
    int years;
    while(scanf("%d", &years) && years !=0)
    {
        printf("%d \n", count_cow(years));
    }
    return 0;
}
uq_90567515217 2年前 回复TA
#include<stdio.h>
int main()
{
	int num,b,y,p,n,a[6]={0,0,0,0},i;
	while(scanf("%d",&n)&&n!=0)
	{	
		for (i=1;i<=4;i++)
		{
			a[i]=0;
		}
		i=1;
		num=1;//母牛数 
		b=1;//可生牛仔的母牛 
		for(y=1;y<=n-1;y++)
		{
			b=b+a[i];
			num=num+b;
			a[i]=b;
			i++;
			if(i==4)
			i=1;
		}
		printf("%d\n",num);
	}
	return 0;
}
终于写出来了,完全不知道啥是递归,硬凑^^
小帅爱比赛 2年前 回复TA
#include<stdio.h>
int way(int n){
	if(n<5){
		return n;
	}
	else{
		return way(n-1)+way(n-3);
	}
}
int main(){
	int n=0;
	scanf("%d\n",&n);
	while(n!=0){
	    int count=way(n);
	    printf("%d\n",count);
		scanf("%d\n",&n);	
	}
	return 0;
}
carrot 2年前 回复TA
@carrot woshishabi
carrot 2年前 回复TA
#include<stdio.h>
int main()
{
    int ex1,ex2,ex3,num1,num2,num3;
    scanf("%d%d%d",&ex1,&ex2,&ex3);
    num1=cowfertility(ex1);
    num2=cowfertility(ex2);
    num3=cowfertility(ex3);
    printf("\n%d\n%d\n%d",num1,num2,num3);
}

int cowfertility(int times)
{
    int num;
    if(times<=4)num=times;
    else
    {
        for(int i=4;i<times;i++)
        {
            num+=times-i;
        }
    }
    return num;
}

//阿ir,为什么不通过啊
oula 2年前 回复TA
真的服了,输出格式都要一样的。打印结果忘记换行,老是说答案错误,搞得我还以为逻辑错了。
#include <stdio.h>
int Get_CowNum(int year);
int main(void)
{
    int year;
    while(1)
    {
        scanf("%d" ,&year);
        if(year == 0)
        {
            break;
        }
        else
        {    
            printf("%d\n" ,Get_CowNum(year));
        }
    }
    return 0;
}

int Get_CowNum(int year)
{
    if(year < 4)
    {
        return year;
    }
    else
    {
        return Get_CowNum(year - 1) + Get_CowNum(year - 3);
    } 
}