经典c程序(0024)---五人问岁

来源:互联网 发布:厄米矩阵的性质 编辑:程序博客网 时间:2024/05/17 22:09
/*************************************************************************************** Function     : test* Create Date  : 2014/04/19* Author       : NTSK13* Email        : beijiwei@qq.com* Copyright    : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。                                              任何单位和个人不经本人允许不得用于商业用途* Version      : V0.1    ***************************************************************************************  经典c程序(0024)题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第   3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后   问第一个人,他说是10岁。请问第五个人多大?**************************************************************************************/#include<stdio.h>#define MY_FUNC  0#if MY_FUNC#define M 5  int get_n_age(int n);//the first solution:int main(){    int n=0,ret=0;n=M;ret=get_n_age(n);printf("The age of %d th person is: %d \n",n,ret);fflush(stdout);//修复Eclipse printf()不能显示的小bugreturn 0;}int get_n_age(int n){if(n==1)return 10;elsereturn get_n_age(n-1)+2;}#else/************************************************************************************///the second solution:int main(){int i=0,n=5,ret=10;for(i=0;i<n-1;i++){ret+=2;}printf("The age of %d th person is: %d \n",n,ret);fflush(stdout);//修复Eclipse printf()不能显示的小bugreturn 0;}#endif


0 0