zoukankan      html  css  js  c++  java
  • HDU 2018 母牛的故事 [补]

    今天刚考完试,和杨曙光玩了RPG,实在不想看题了

    /***************************************************/

    母牛的故事

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


    Problem Description
    有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
     
    Input
    输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
    n=0表示输入数据的结束,不做处理。
     
    Output
    对于每个测试实例,输出在第n年的时候母牛的数量。
    每个输出占一行。
     
    Sample Input
    2 4 5 0
     
    Sample Output
    2 4 6
     
    Author
    lcy
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2050 1297 2190 2049 2501
     
    一次a的不过用的方法很麻烦,我是分成大,小3,小2,小1,3种母牛。大,小3,小2,小1看成一组,叫4小组,大,小2,小1看成一种组,叫3小组,大,小1叫2小组,然后各用一个数组。通过上一年与下一年的关系推出最后的结果。最后我都用c数组表示了,a,b数组消掉了。
     1 #include<math.h>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 __int64 c[60];
     9 int n;
    10 int main()
    11 {
    12     c[3]=c[2]=c[1]=0;
    13     c[4]=1;c[5]=1;c[6]=1;c[7]=2;c[8]=3;
    14     for(int i=9;i<56;i++)
    15     {
    16         c[i]=c[i-3]+c[i-1];
    17     }
    18     n=1;
    19     while(n<20)
    20     {
    21         if(n==1){cout<<1<<endl;n++;continue;}
    22         if(n==2){cout<<2<<endl;n++;continue;}
    23         if(n==3){cout<<3<<endl;n++;continue;}
    24 //        printf("%d
    ",c[n]);
    25         printf("%d
    ",4*c[n]+3*c[n-2]+2*c[n-1]);
    26         n++;
    27     }
    28     return 0;
    29 }
     在网上看了别人的代码,两种方法都比我的简单,
     1 #include<math.h>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 __int64 c[60];
     9 int n;
    10 int main()
    11 {
    12     c[1]=1,c[2]=2,c[3]=3,c[4]=4;
    13     for(int i=5;i<56;i++)
    14     {
    15         c[i]=c[i-3]+c[i-1];
    16     }
    17     while(~scanf("%d",&n),n)
    18     {
    19         printf("%d
    ",c[n]);
    20     }
    21     return 0;
    22 }
     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int n;
     6     while(cin>>n)
     7     {
     8         if(n==0)break;
     9         int a,b,c,d;   //分别代表一岁二岁三岁四岁及其以上的母牛的数目。
    10         if(n<=4)cout<<n<<endl;   //*若n<=4,很容易得出总数为n
    11         else{
    12             n=n-4;
    13             a=b=c=d=1;
    14             for(int i=0;i<n;i++)
    15             {
    16                 d=c+d;//*当年大母牛数目d为前一年c和d的总和
    17                 c=b;//*b母牛长了一岁到了c
    18                 b=a;//*同上
    19                 a=d;//*长成的母牛和原本的母牛共生出新的小母牛a个
    20             }
    21             cout<<a+b+c+d<<endl;
    22         }
    23     }
    24 }
  • 相关阅读:
    Emacs教程
    华为上机测试 2015
    奇偶排序
    C语言中的EOF和回车不一样
    jquery 使用方法
    1116
    1115
    1109
    Oracle14~23
    get与post的区别
  • 原文地址:https://www.cnblogs.com/wmxl/p/4687773.html
Copyright © 2011-2022 走看看