首页 > 编程 > C++ > 正文

[华为OJ--C++]029-统计每个月兔子的总数

2019-11-08 01:44:59
字体:
来源:转载
供稿:网友

题目描述:

有一对兔子,从出生后第3个月起每个月都生一对兔子,一对小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少对?

 输入描述:输入int型表示month

输出描述:输出兔子总数int型

输入例子:9

输出例子:34

算法实现:

#include<iostream>  #include<string>#include<vector>using namespace std;    //************************************************    // * Author: 赵志乾    // * Date: 2017-2-20     // * Declaration: All Rigths Reserved !!!    //***********************************************/  int main()  {  	int month;	cin>>month;	int a1=1,a2=1,a3=2;	int ret;	switch(month)	{		case 1:		case 2: cout<<1<<endl;break;		case 3: cout<<2<<endl;break;		default:			for(int i=4;i<=month;i++)			{				ret=a3+a2;				a2=a3;				a3=ret;			}			cout<<ret<<endl;	}		    return 0;  }  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选