UVa 12459 - Bees' ancestors

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

題目網址
中文網址

Solution

費氏數列:

M:male, F:female

Both male and female need a mom: F(n) = F(n-1) + M(n-1)
Only female need a dad: M(n) = F(n-1)
F(n) = F(n-1) + F(n-2)

n generation:
M(n) + F(n) = F(n-1) + F(n)

Code

UVa 12459
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
int main()
{
long long fib[81] = {1, 1};
int n;
for (int i = 2; i < 81; i++)
fib[i] = fib[i - 1] + fib[i - 2];

while (scanf("%d", &n) && n)
printf("%lld\n",fib[n]);

return 0;
}