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?
200.00 6.5 300 500 4 1000.00
7 18
给出本金,年利率(百分之),目标存款
存款 = 本金 * (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;
}