【原创】该存多少钱之递归法

代码
1 /*
2 * 假设银行一年整存零取的月息为0.63%。现在某人手中有一笔钱,
3 * 他打算在今后的五年中的年底取出1000元,到第五年时刚好取完,
4 * 请算出他存钱时应存入多少?
5 *
6 * 因为按照本题有如下公式:
7 * 当1<=n<=4 时
8 * f(n) =( f(n+1)+1000) / (1+0.0063*12)
9 * 当 n=5 时
10 * f(n) = 1000/(1+0.0063*12)
11 * 所以可以采用递归的算法
12  */
13 #include <stdio.h>
14 #include <assert.h>
15  float getTotal(int year)
16 {
17 if(year==5)
18 return 1000/(1+0.0063*12);
19 else
20 return (getTotal(year+1)+1000)/(1+0.0063*12);
21 }
22 int
23 main(void)
24 {
25 int i;
26 float total=0;
27 total = getTotal(1);
28 printf("他的首次存款额最少为 %.2f ?\n",total);
29
30 return 0;
31 }
32

 

 

Output:
1
 他的首次存款额最少为 4039.44 元
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/asiainfolf/archive/2010/09/16/5887357.aspx

posted on 2010-09-17 00:07  sohu2000000  阅读(225)  评论(0编辑  收藏  举报

导航