題解1236:母牛生小牛 (Java描述)

acDream_發表於2018-06-22

解題思路:找規律  1 1 1 2 3 4 6 9 13

注意事項:

參考程式碼:

public class Main {


public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int N = in.nextInt();

int [] cowsNum = new int [50];

for(int i = 0;i<3;i++) {

cowsNum[i] = 1;

}

cowsNum[3] = 2;

cowsNum[4] = 3;

cowsNum[5] = 4;

for(int i=6;i<cowsNum.length;i++) {

cowsNum[i] = cowsNum[i-1] + cowsNum[i-3];

}

System.out.println(cowsNum[N-1]);

}


}


相關文章