题目:
波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要说明你选择的理由。
- #include <stdio.h>
- int Pheponatch(int);
- int main()
- {
- printf("The 10th is %d",Pheponatch(10));
- return 0;
- }
- int Pheponatch(int N){
- --------------------------------
- | |
- | |
- --------------------------------
- }
- #include <stdio.h>
- int Pheponatch(int);
- int main(void){
- int Max;
- scanf("%d",&Max);
- printf("The 10th is %d/n",Pheponatch(Max));
- return 0;
- }
- int Pheponatch(int N){
- if(N==1||N==2)
- return 1;
- else
- return Pheponatch(N-1)+Pheponatch(N-2);
- }
编译环境:
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
运行过程:
[explover@Explover 20081101]$ gcc -g phep.c -o phep[explover@Explover 20081101]$ ./phep10The 10th is 55
(本人水平有限,不足之处,请大家多多指正,谢谢!)