博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 11000- Bee 递推
阅读量:5107 次
发布时间:2019-06-13

本文共 1726 字,大约阅读时间需要 5 分钟。

In Africa there is a very special species of bee. Every year, the female bees of such species give birth

to one male bee, while the male bees give birth to one male bee and one female bee, and then they die!
Now scientists have accidentally found one “magical female bee” of such special species to the effect
that she is immortal, but still able to give birth once a year as all the other female bees. The scientists
would like to know how many bees there will be after N years. Please write a program that helps them
find the number of male bees and the total number of all bees after N years.
Input
Each line of input contains an integer N (≥ 0). Input ends with a case where N = −1. (This case
should NOT be processed.)
Output
Each line of output should have two numbers, the first one being the number of male bees after N
years, and the second one being the total number of bees after N years. (The two numbers will not
exceed 232.)
Sample Input
1
3
-1
Sample Output
1 2
4 7

 

题意: 蜜蜂,每年每只雄蜂产下一只雌蜂和一只雄蜂,每只雌蜂产下一只雄蜂,然后就死去, 现在发现了一只不会死的雌蜂,问以她为起始点,第N年有多少雄蜂和一共多少蜜蜂。

题解:    设第i年的雄蜂和雌蜂分别为a(i)与s(i),则有如下递推关系:

            1 s(i)= a(i-1)+ 1

            2 a(i)= s(i-1)+ a(i-1)

            整理得:a(i)=a(i-1)+ a(i-2)+ 1;

#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std ;typedef long long ll;const int N=100+10;const int maxn = 1e9 + 1;ll a[N];void init() { a[1] = 1; a[0] = 0; for(int i=2;i<=N;i++) a[i] = a[i-1] + a[i-2] +1ll;}int main() { init(); int n; while(~scanf("%d",&n)) { if(n == -1) break; printf("%lld %lld\n", a[n] , a[n+1]); } return 0;}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/5124314.html

你可能感兴趣的文章
Atomic
查看>>
div 显示滚动条与div显示隐藏的CSS代码
查看>>
Redis-1-安装
查看>>
Access denied for user ''@'localhost' to database 'mysql'
查看>>
微信公众号里面使用地图导航
查看>>
部署支持 https 的 Nginx 服务
查看>>
‘Cordova/CDVPlugin.h’ file not found
查看>>
WebAssembly是什么?
查看>>
C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件)...
查看>>
树状数组_一维
查看>>
【拓扑排序】【最短路】【最小生成树】Day 9.2
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
linux install ftp server
查看>>
C# 使用 Abot 实现 爬虫 抓取网页信息 源码下载
查看>>
嵌入式软件设计第8次实验报告
查看>>
NP难问题求解综述
查看>>
算法和数据结构(三)
查看>>
看一下你在中国属于哪个阶层?
查看>>
在iOS 8中使用UIAlertController
查看>>
Ubuntu下的eclipse安装subclipse遇到没有javahl的问题...(2天解决了)
查看>>