奶牛问题
Description
好好過個年吧,遇雪災(zāi)了, 好好火炬?zhèn)鬟f,遇藏獨(dú)了 , 發(fā)展農(nóng)村醫(yī)療吧,發(fā)手足口病了 買點(diǎn)股票吧,大小非減持了 做火車吧,還出軌了 在家待著吧,還地震了 天將降大運(yùn)于斯國也,必先下其大雪,撞其火車,搶其火炬,震其國土,漲其物價,跌其股市,空房其民。 四川大地震,牽動了中華民族的很多顆心。小明決定用實(shí)際行動來支援四川,所以他決定養(yǎng)奶牛給前線運(yùn)牛奶。 最初他只有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請編程實(shí)現(xiàn)在第n年的時候,共有多少頭母牛?
Input
輸入數(shù)據(jù)由多個測試實(shí)例組成,每個測試實(shí)例占一行,包括一個整數(shù)n(0 < n < 55),n的含義如題目中描述。 n=0表示輸入數(shù)據(jù)的結(jié)束,不做處理。
Output
對于每個測試實(shí)例,輸出在第n年的時候母牛的數(shù)量。 每個輸出占一行。
Sample Input
2 4 5 0Sample Output
2 4 6HINT
題解:先預(yù)處理一下,不然TLE
#include <iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long a[100]; void nn(){a[1]=1;a[2]=2;a[3]=3;a[4]=4;for(int i=5;i<=56;i++){a[i]=a[i-1]+a[i-3];} } int main(int argc, char *argv[]) {int n; nn();while(scanf("%d",&n)!=EOF){if(n==0)break;printf("%ld\n",a[n]);} return 0; }?
總結(jié)
- 上一篇: 我开始方了
- 下一篇: Vasya and Book