hdu 2018 母牛的故事

2021-08-16 21:49:46 字數 783 閱讀 5108

problem description

有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請程式設計實現在第n年的時候,共有多少頭母牛?

input

輸入資料由多個測試例項組成,每個測試例項佔一行,包括乙個整數n(n<55),n的含義如題目中描述。

n=0表示輸入資料的結束,不做處理。

output

對於每個測試例項,輸出在第n年的時候母牛的數量。

每個輸出佔一行。

sample input

2 4 5 0

sample output

2 4

6

#include

#include

#include

#include

using

namespace

std;

/***菲波拉契數列

函式遞推式:

* 第n年牛的總數等於n-1年的母牛數加上第n年新增的母牛數,

* 第n年只有n-3年的那些母牛才能產母牛,所以第n年的母牛數等於第n-1和n-3年(新增)的母牛數的和,

f(n)=n n<=4

f(n)=f(n-1) + f(n-3) n>4

編寫遞迴函式即可實現***/

int cow(int n)

else

}int main()

printf("%d\n",cow(n));

}return

0;}

hdu 2018 母牛的故事

include main while scanf d n eof n 0 printf d n a n problem description 有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請程式設計實現在第n年的時候,共有多少頭母牛?input 輸入資料由...

hdu 2018 母牛的故事

problem description 有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請程式設計實現在第n年的時候,共有多少頭母牛?input 輸入資料由多個測試例項組成,每個測試例項佔一行,包括乙個整數n 0 output 對於每個測試例項,輸出在第n年的...

HDU 2018 母牛的故事

自己找的遞推規律,ac include include include include include include include include using namespace std typedef long long ll const int mod 10000007 const int ...