Savings Account

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 300
500 4 1000.00
Sample Output
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值