lucifer


私信TA

用户名:13379476814

访问量:14105

签 名:

等  级
排  名 3088
经  验 1957
参赛次数 0
文章发表 22
年  龄 20
在职情况 学生
学  校 西北工业大学明德学院
专  业

  自我简介:

第?年1岁牛2岁牛3岁牛4岁牛牛的总数
100011
210012
311013
411114
521126
632139
7423413

仔细一看,发现与斐波拉契数列相似。

第n年的牛的数量总数第n-1年牛的数量+第n-3年牛的数量(n>=4)

所以建立个数组就行,代码如下1543935970(1).png

 

0.0分

11 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

public class Main {
      
	public int count = 0;
	
	public void born(int sheepSum,int n,int year) {
		if (n > 4) {
			sheepSum++;
		}
		if (n <= year) {
		this.count = this.count + sheepSum;
		born(sheepSum,n+1,year);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
         Scanner input = new Scanner(System.in);        
         int n = input.nextInt();
         while(n != 0) {
        	 Main main = new Main();
        	 main.born(1,1,n);
        	 System.out.println(main.count);
        	 n = input.nextInt();
         }
	}

}
为什么错了一半的数据??
2020-04-03 11:39:49
第七年错了  第二岁应该是三头  第三岁应该是两头
2020-01-31 18:30:58
import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int a=in.nextInt();
            if (a==0)
            {
                break;
            }
            else if (a<=4)
            {
                System.out.println(a);
            }
            else {
                int b =a;
                for (int i = 1; i <= a-4; i++) {
                    b+=i;
                }
                System.out.println(b);
            }
        }
    }
}


我这个出什么问题了
2019-12-26 17:20:05
我总觉得第一年应该是两头牛
,不是说每年年初初始的牛都会生产一头牛嘛,那初始为1,第一年的时候应该是2啊
2019-07-27 11:19:09
  • «
  • 1
  • »