解题思路:
第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 人评分
#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; }
#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; }
#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; } 终于写出来了,完全不知道啥是递归,硬凑^^
#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; }
#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,为什么不通过啊
carrot 2022-06-30 15:31:24 |
woshishabi
真的服了,输出格式都要一样的。打印结果忘记换行,老是说答案错误,搞得我还以为逻辑错了。 #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); } }
动态规划和递归+剪枝有什么区别吗,求解答
C语言程序设计教程(第三版)课后习题5.5 (C语言代码)浏览:556 |
逆反的01串 (C++代码)(依旧推荐switch)浏览:936 |
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:670 |
矩阵转置 (C语言代码)浏览:1525 |
C语言训练-素数问题 (C语言代码)浏览:998 |
WU-蓝桥杯算法提高VIP-Quadratic Equation (C++代码)浏览:1752 |
用筛法求之N内的素数。 (C语言代码)浏览:649 |
K-进制数 (C语言描述,蓝桥杯)浏览:925 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:1465 |
简单的a+b (C语言代码)浏览:580 |