12459 - Bees' ancestors

Background

Maya is a bee that likes to help her friends. Willy is a drone bee and Maya's best friend. He has just discovered that he has no father, and he is worried about that.


The drone bee Willy.

The Problem

Maya knows that female bees have two parents (one father and one mother), but male or drone bees have a mother but no father. This is because if an egg is laid by an unmated female, it hatches a drone bee, but if, instead, an egg was fertilized by a male, it hatches a female.

After Maya talks with Willy about this, he begins to think about the number of ancestors that he has. He has one mother. He also has two grandparents (one grandmother and one grandfather). And he has three great-grandparents. Since Willy is very lazy, he doesn't want to perform more calculations and he is asking you to write a program that, given a number of generation, determines how many of Willy's ancestors belong to that generation, under the assumption that the ancestors at each level are unrelated.

The Input

Your program receives a sequence of positive integers, one per line, each representing the generation. The maximum value for the generation is 80. The input terminates with a 0.

The Output

For each input case, your program must print the corresponding number of ancestors that Willy has in such generation.

Maya 知道雌蜂有双亲(一个爸爸和一个妈妈),但是雄蜂则只有一个妈妈而没有爸爸。这是因为未交配的雌蜂所产的卵会孵出雄蜂,但是受精的卵则会孵出雌蜂。

在Maya 晓以大义之后,Willy 开始好奇他有多少祖先。他有一个妈妈,两个祖父母(一个祖父和一个祖母)。他也有三个曾祖父母。因为Willy 很懒,不想做太多计算,他要请你写个程式来帮他计算某一代的祖先一共有几个。假设同一代的祖先之间没有亲戚关系。

Sample Input

1
2
3
0

Sample Output

1
2
3

解题思路:主要是找出规律

#include<stdio.h>
int main()
{long n,a,b,c,i;
while(scanf("%ld",&n)!=EOF){
                           a=1;
                           b=2;
                           if(n==0)break;
                           if(n==1)
                           printf("1\n");
                           else if(n==2)
                           printf("2\n");
                           else if(n>2){
                                for(i=0;i<n-1;i++){
                                                   a=a+b;
                                                   c=a;
                                                   a=b;
                                                   b=c;
                                                   }
                                printf("%ld\n",a);
                                }
                           }
return 0;
}

 

posted on 2013-02-08 19:23  喂喂还债啦  阅读(315)  评论(0编辑  收藏  举报