题目:Daphne以10%的单利息投资了100美元(也就是说,每年投资获利相当于原始投资的10%)。Deirdre以5%的复合利息投资了100美元(也就是说,利息是当前余额的5%,包含之前的利息)。编写一个程序,计算需要多少年Deirdre的投资额才会超过Daphne,并显示那时两人的投资额。
#include<stdio.h>
#define RATE_SIMP 0.10 //每年获利=原始投资*10%
#define RATE_COMP 0.05 //每年获利=当前余额*5%
#define INIT_SIMP 100 //Daphne 以 $100 作为单利率的原始投资
#define INIT_COMP 100 //Deirdre以 $100 作为复合利率的原始投资
int main()
{
int years = 0;
double simp_daphne = INIT_SIMP;
double comp_deirdre = INIT_COMP;
while(comp_deirdre <= simp_daphne)
{
simp_daphne += INIT_SIMP * RATE_SIMP;
comp_deirdre += comp_deirdre * RATE_COMP;
years++;
}
printf("It will take %d years for Deirdre to surpass Daphne.\n",years);
printf("At this point, Daphne's investment is $%.2f\n",simp_daphne);
printf("At this point, Deirdre's investment is $%.2f\n",comp_deirdre);
return 0;
}
运行结果: