题解 | 统计每个月兔子的总数

统计每个月兔子的总数

https://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //初始化,nums表示第i月兔子总数,news表示第i月新生兔总数
        int []nums=new int [32];
        int []news=new int [32];
        nums[1]=1;news[1]=0;
        nums[2]=1;news[2]=0;
        nums[3]=2;news[3]=1;
        for(int i=4;i<32;i++){
            nums[i]=nums[i-1]+1+Arrays.stream(Arrays.copyOfRange(news,1,i-1)).sum();
            news[i]=1+Arrays.stream(Arrays.copyOfRange(news,1,i-1)).sum();
        }
        int m=sc.nextInt();
        System.out.print(nums[m]);
    }
    
  
}

全部评论
点赞 回复 分享
发布于 11-05 15:05 陕西

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务