zoukankan      html  css  js  c++  java
  • hdu 2018 母牛的故事

    母牛的故事

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 66390    Accepted Submission(s): 33034

    Problem Description
    有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
     
    Input
    输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。 n=0表示输入数据的结束,不做处理。
     
    Output
    对于每个测试实例,输出在第n年的时候母牛的数量。 每个输出占一行。
     
    Sample Input
    2 4 5 0
     
    Sample Output
    2 4 6
    解法一:
     1 #include <iostream>
     2 using namespace std;
     3 
     4 int cow(int n)
     5 {
     6     if(n<=4)
     7         return n;
     8     else
     9         return cow(n-1)+cow(n-3);
    10 }
    11 int main()
    12 {
    13     int n;
    14     while(cin >> n && n)
    15        cout << cow(n) << endl;
    16     return 0;
    17 }
    View Code

    解法二:

     1 #include <iostream>
     2 using namespace std;
     3 int dp[55];
     4 int main() {
     5     int  n;
     6     dp[1] = 1;
     7     for (int i = 2; i < 55; i++){
     8         if ( i < 5)
     9             dp[i] = i;
    10         else
    11             dp[i] = dp[i-1] + dp[i-3];
    12     }
    13     
    14     while(cin >> n && n != 0){
    15         cout << dp[n] << endl;
    16     }
    17         
    18     //system("pause");
    19     return 0;
    20 }
    View Code
    越努力,越幸运
  • 相关阅读:
    analysis of algorithms
    Measurement of Reflected Radiation
    lecture 5
    lecture 3
    字符串
    Emission of Radiation辐射发射
    Electromagnetic Radiation(EMR) 电磁辐射
    Linux FTP服务器-VSFTPD虚拟用户配置
    jenkins notes
    python nose使用记录
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5483382.html
Copyright © 2011-2022 走看看