Farmer John made a profit last year! He would like to invest it well but wonders how much money he will make. He knows the interest rate R (an integer between 0 and 20) that is compounded annually at his bank. He has an integer amount of money M in the range 100..1,000,000. He knows how many years Y (range: 0..400) he intends to invest the money in the bank. Help him learn how much money he will have in the future by compounding the interest for each year he saves. Print an integer answer without rounding. Answers for the test data are guaranteed to fit into a signed 32 bit integer.
Input
* Line 1: Three space-separated integers: R, M, and Y
Output
* Line 1: A single integer that is the number of dollars FJ will have after Y years.
Sample Input
5 5000 4
Sample Output
6077
Hint
INPUT DETAILS:
5% annual interest, 5000 money, 4 years
OUTPUT DETAILS:
Year 1: 1.05 * 5000 = 5250
Year 2: 1.05 * 5250 = 5512.5
Year 3: 1.05 * 5512.50 = 5788.125
Year 4: 1.05 * 5788.125 = 6077.53125
The integer part of 6077.53125 is 6077.
思路:
此题本为水题,只是我的基础知识不牢固导致一直WA(QAQ)。
Hint:
1.实型数据使用double。(float精度不够会WA)
2.输出时需强制转换为int型,不能使用”%.0lf\n”输出
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double r;
int m,y;
double sum;
scanf("%lf%d%d",&r,&m,&y);
r=1.0+r/100.0;
sum=m*pow(r,y); //pow() 函数用来求x(r)的y(y)次幂(次方)
//x、y及函数值都是double型 ,其原型为:double pow(double x, double y);
printf("%d\n",(int)sum); //将float型强制转换为int型输出
return 0;
}