龚秋志


私信TA

用户名:uq_48078956563

访问量:49220

签 名:

等  级
排  名 33
经  验 13339
参赛次数 30
文章发表 64
年  龄 19
在职情况 学生
学  校 湖北生物科技职业学院
专  业 计算机应用

  自我简介:

解题思路:

        第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分

177 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

感谢大佬分享
2022-10-06 13:48:54
#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;
}
2022-09-28 15:51:53
#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;
}
2022-09-28 15:51:15
#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;
}
终于写出来了,完全不知道啥是递归,硬凑^^
2022-09-23 17:59:32
#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;
}
2022-07-07 10:59:23
#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,为什么不通过啊
2022-06-30 13:19:40
真的服了,输出格式都要一样的。打印结果忘记换行,老是说答案错误,搞得我还以为逻辑错了。
#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);
    } 
}
2022-02-27 17:31:44
动态规划和递归+剪枝有什么区别吗,求解答
2022-01-19 11:17:13