首页 > 其他 > 详细

《编程题》穷举法求N年后有多少头牛

时间:2016-07-17 12:49:33      阅读:238      评论:0      收藏:0      [点我收藏+]

若一头小母牛,从出生起第四个年头开始每年生一头母牛,按这个规律,第N年时有多少头母牛?

 

 1 #include <iostream>
 2 
 3 int main(int argc, const char * argv[]) {
 4 
 5     int N = 0;
 6 
 7     std::cout<<"请输入年份\n";
 8     std::cin>>N;
 9     int *iA;
10     
11     if((iA = (int *)malloc(N*sizeof(int)))==NULL)
12     {
13         return 0;
14     }
15 
16     int theResult = 0;
17     
18     
19     for(int i=0;i<N;++i)
20     {
21         iA[i] = 0;
22     }
23     
24     for(int i=0;i<N;++i)
25     {
26         theResult+=1;
27         for(int j=0;j<=i;++j)
28         {
29             iA[j] += 1;
30             if(iA[j]>=4)
31                 theResult += 1;
32         }
33     }
34     
35     std::cout << theResult << "\n";
36     free(iA);
37     return 0;
38 }

 

《编程题》穷举法求N年后有多少头牛

原文:http://www.cnblogs.com/slowx/p/5677498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!