UVa 12459 - Bees' ancestors
来源:互联网 发布:网络推销产品 编辑:程序博客网 时间:2024/06/12 20:12
题目:一只雌蜂有一个父亲和母亲,一只雄蜂只有一个母亲,问一只雄蜂的第n带祖先有多少个。
分析:递推,Fib数列。
状态定义:设f(k)和m(k)分别为第k代祖先中雌蜂和雄蜂的数量;
递推关系:f(k)= f(k-1)+ m(k-1)和 m(k)= f(k-1);
递推整理:f(k)= f(k-1)+ f(k-2);f(1) = 1;f(2)= 2;
说明:使用long long防止溢出。
#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;long long F[100];int main(){F[1] = 1; F[2] = 2;for (int i = 3; i < 100; ++ i)F[i] = F[i-1] + F[i-2];int n;while (cin >> n && n)cout << F[n] << endl; return 0;}
0 0
- UVa 12459 - Bees' ancestors
- Bees can bite as well as sting
- .ancestors *效果
- Closest Common Ancestors
- POJ1330 Nearest Common Ancestors
- POJ1330(Nearest Common Ancestors)
- poj_1330Nearest Common Ancestors
- poj_1470Closest Common Ancestors
- POJ1330--Nearest Common Ancestors
- POJ1470--Closest Common Ancestors
- Nearest Common Ancestors
- poj1470 Closest Common Ancestors
- POJ Nearest Common Ancestors
- poj_1330 Nearest Common Ancestors
- Nearest Common Ancestors
- poj1470 Closest Common Ancestors
- poj1330 Nearest Common Ancestors
- Least Common Ancestors
- springmvc搭框架的第一步
- C++日志类
- android中调用支付宝
- 第二题 P067
- Reverse Words in a String -- leetcode
- UVa 12459 - Bees' ancestors
- 第三题 P067
- Spring学习笔记之Spring事务及测试
- 漫谈程序员系列:她发现了一个Bug……
- 黑马程序员——JAVA数组
- 内存分配和常见错误——高质量C、C++编程指南
- 加载与链接器的小知识
- Andorid wifi简记
- 第十四周项目1——小玩文件(1)统计字符个数