没有java题解我就来写一个吧
这题就很像兔子繁殖那题了,简单dp可以解决,题问的成虫不包括幼虫,分下两个数组就好了
思路:
1、建立两个数组,成虫数组old,幼虫数组young
2、前面x-1天,成虫没有产出幼虫,所以成虫还是第一天的1,幼虫为0
3、到第x天,成虫产出一次幼虫,young[i]=old[i-x]*y 。成虫数量为前面一天的成虫数量和2天之前产生的幼虫数量 old[i]=old[i-1]+young[i-2](判段一下如果当前是第一次产生幼虫的前两天是不会产生新的成虫的。
4、输出第z天的old[z]
import java.io.*; public class Main{ static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException { //读取数据 String[] temp=bf.readLine().split(" "); int x=Integer.parseInt(temp[0]); int y=Integer.parseInt(temp[1]); int z=Integer.parseInt(temp[2]); //创建数组 long young[] =new long[z+1]; long old[]=new long[z+1]; //循环前x-1天 for(int i=0;i<x;i++) { young[i]=0; old[i]=1; } //循环第x天到第z天 for(int i=x;i<=z;i++) { //思路第三部 young[i]=old[i-x]*y; if(i<2) old[i]=old[i-1]; else old[i]=old[i-1]+young[i-2]; } //输出 pw.print(old[z]); pw.flush(); } }
0.0分
2 人评分
简单的a+b (C语言代码)浏览:521 |
最小公倍数 (C语言代码)浏览:863 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:655 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:451 |
printf基础练习2 (有点不明白)浏览:841 |
DNA (C语言代码)浏览:540 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:486 |
罗列完美数 (C语言代码)浏览:491 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:611 |
1392题解(大数相加)浏览:603 |