新浪博客 发表时间 -- 2009-07-27 16:38:21)
题目:
Suppose you open a savings account with a certain initial balance. You will not make any withdrawals or further deposits for a number of years. The bank will compound your balance (add the annual interest) once a year, on the anniversary of the opening of the account. Your goal is to achieve a certain target amount in your savings account.
In how may years will the target amount be achieved?
In how may years will the target amount be achieved?
Input:<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr> The input file will contain data for one or more test cases, one test case per line. Each line will contain three numbers: the initial balance, the annual interest rate (as a percentage of the balance), and the target amount, separated by blank spaces. These will be positive numbers; they may or may not contain a decimal point.<br> The target amount will be greater than the initial balance. The input is terminated by end-of-file</wbr></wbr></wbr></wbr>
Outp:
<wbr><wbr><wbr> For each line of input, your program will produce exactly one line of output: This line will contain one positive integer value: the number of years required to achieve the target amount.</wbr></wbr></wbr>
数据......
Sample Input:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 200.00 6.5 300<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 500 4 1000.00</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
Sample Output:
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 7</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 18</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
算法:
<wbr><wbr><wbr><wbr><wbr> 虽然说英文不太懂,但一看到这个题目就大致猜到题意了,输入的数据是本金,利率,和最终所得的金额,求的是经过多少年能达到总金额,也就是第三个数.这应该是道很简单的初中数学题,幸好,还记得那个公式,一想到这就有点兴奋了,赶紧奋笔疾书...额,想想...P=(1+r%)^N*S...</wbr></wbr></wbr></wbr></wbr>
P为总金额,r为每年的利润率,S为本金..有这公式就好办了...
代码:
<wbr><wbr><wbr><wbr> #include "stdio.h"<br><wbr><wbr> #include "math.h"<br> int main()<br> {<br><wbr>int year;<br><wbr>float initial,rate,value,t,rate1;<br><wbr>while (scanf("%f %f %f",&initial,&rate,&value)!=EOF)<br><wbr>{<br><wbr><wbr>year=0;<br><wbr><wbr>rate1=rate/100;<br><wbr><wbr>t=value/initial;<br><wbr><wbr>year=log10(t)/log10(1+rate1);<br><wbr><wbr>printf("%d\n",year+1);<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>