解题思路:
先手动计算每年的牛的数量:
1:1
2:2
3:3
4:4
5:6
6:9
7:13
根据题意找规律可得:
本年度可生崽的牛,均为三年前就存在的牛
因此:
本年的牛数=去年牛的数量+三年前牛的数量
注意事项:
参考代码:
#include<iostream>
using namespace std;
int main()
{
int n = 0;
cin >> n;
while (n)
{
if ((n < 55) && (n > 0))
{
long threeYearsAgoCows = 1, twoYearsAgoCows = 1, oneYearsAgoCows = 1, nowTatle = 1;
if (n == 1)
nowTatle = 1;
else
for (int i = 2; i <= n;i++)
{
threeYearsAgoCows = twoYearsAgoCows;
twoYearsAgoCows = oneYearsAgoCows;
oneYearsAgoCows = nowTatle;
nowTatle += threeYearsAgoCows;
//cout << "i: " << i << " 三年前:" << threeYearsAgoCows << " 两年前:" << twoYearsAgoCows << " 一年前:" << oneYearsAgoCows << " 现在:" << nowTatle << endl;
}
//cout << "---------------- 现在:" << nowTatle << " ----------------" << endl << endl;
cout << nowTatle << endl;
}
cin >> n;
}
return 0;
}
0.0分
1 人评分
不容易系列2 (C语言代码)浏览:641 |
A+B for Input-Output Practice (II) (C语言代码)浏览:1045 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:485 |
1011题解浏览:819 |
C语言程序设计教程(第三版)课后习题9.10 (C语言代码)浏览:866 |
printf基础练习2 (C语言代码)浏览:547 |
简单的a+b (C语言代码)浏览:542 |
数列排序 (C语言代码)浏览:674 |
字符逆序 (C语言代码)浏览:541 |
神奇的fans (C语言代码)浏览:1125 |