2

我的任务是让那些赢得 100 万美元头奖的人将其存入银行,并从他的钱中赚取 8% 的年利率。现在,在每年结束之前,他都会拉 100k。那么他需要多少年才能清空他的帐户?

这是我的代码:

#include <stdio.h>
long int year_ended(long int prize);//declare the function to be implemented 


int main(void)

{

    long int jackpot, years;

    jackpot = 1000000;

    for (years = 0; jackpot >= 0; years++) //counting the years until the jackpot is 0
        year_ended(jackpot); 

    printf("it will take %ld years to empty the account", years);

    return 0;
}

long int year_ended(long int prize) //function to decrement 100k every year and add the erned interest

{
    double yearlyRate = 0.08;

    int YearlyWithdraws = 100000, left;

    left = (prize - YearlyWithdraws) * yearlyRate + (prize - YearlyWithdraws);

    prize = left;

    return prize;
}

错误是:程序继续运行,但我没有输出..

我究竟做错了什么?

4

1 回答 1

3
for (years = 0; jackpot >= 0; years++)
    year_ended(jackpot); 

这是一个无限循环,因为条件始终为真。您可能想要修改jackpot. 就像是:

for (years = 0; jackpot >= 0; years++) 
    jackpot = year_ended(jackpot); 
于 2013-01-27T12:15:06.170 回答