Savings Account

来源:互联网 发布:大学生电脑软件推荐 编辑:程序博客网 时间:2024/06/05 08:45
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?

Input
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. The target amount will be greater than the initial balance. The input is terminated by end-of-file
Output
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.
Sample Input
200.00 6.5 300500 4 1000.00
Sample Output
718
题意:

给出本金,年利率(百分之),目标存款

存款 = 本金 * (1+年率)^  i (i 为存款年限)

代码:

#include<stdio.h>#include<math.h>#include<string.h>using namespace std;double a,p,b;int F(){int i=0;double sum=a;while(1){if(sum>=b) break;sum=a*pow(1+p*0.01,i);//printf("%.3f\n",sum);i++;}return i;}int main(){while(scanf("%lf%lf%lf",&a,&p,&b)!=EOF){printf("%d\n",F()-1);}return 0;}


原创粉丝点击