acm 2018 java

來源:互聯網
上載者:User

標籤:

Problem Description
 
有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請編程實現在第n年的時候,共有多少頭母牛?
 
 
 
 
Input
輸入資料由多個測試執行個體組成,每個測試執行個體佔一行,包括一個整數n(0<n<55),n的含義如題目中描述。
n=0表示輸入資料的結束,不做處理。
 
 

Output
對於每個測試執行個體,輸出在第n年的時候母牛的數量。
每個輸出佔一行。
 
 

Sample Input
2 4 5 0
 
 
 

Sample Output
2 4 6   
 
import java.util.Scanner;/** * Created by Administrator on 2016/4/4. */public class Main {    public static void main(String args[]) {        Scanner in = new Scanner(System.in);        for (; ; ) {            int n = in.nextInt();            if (n == 0) {                System.exit(0);            } else if (n > 0) {                int a = 0, b = 0, c = 0;                int d = 1;                int sum = 0;                for (int i = 1; i < n; i++) {                    d = d + c;                    c = b;                    b = a;                    a = d;                }                sum = a + b + c + d;                System.out.println(sum);            }        }    }}

 

acm 2018 java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.