POJ 2390 Bank Interest(我的水题之路——double和floa计算差别)

Bank Interest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11864   Accepted: 7071

Description

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.

Source

USACO 2004 November

对于R%的年利率,本金为M,一共存Y年,问Y年之后本金+利润一共多少钱?

模拟。

注意点:
1)如果用double,中间值value变化情况如下(除了输出样例意外的输出,见解题代码的注释部分):
5 5000 4
1 5250.000000
2 5512.500000
3 5788.125000
4 6077.531250
6077
2)如果用float,中间值value变化情况如下:
5 5000 4
1 5250.000000
2 5512.499512
3 5788.124023
4 6077.529785
6077
如上,在比较小的数的计算的时候,用double计算更为精确。

代码(1AC):
#include <cstdio>
#include <cstdlib>

int main(void){
    int r, m, y;
    int i;
    double value, rate;

    while (scanf("%d%d%d", &r, &m , &y) != EOF){
        rate = 1 + (double)r / 100.0;
        value = m;
        for (i = 0; i < y; i++){
            value *= rate;
            //printf("%d %lf\n", i + 1, value);
        }
        printf("%d\n", (int)value);
    }
    return 0;
}



你可能感兴趣的:(POJ 2390 Bank Interest(我的水题之路——double和floa计算差别))