这就是正在讨论的程序:
#include <stdio.h>
#include <tgmath.h>
#include <simpio.h>
long main(){
long cars_t,years;
cars_t=80000;
years=0;
while (cars_t<=160000){
cars_t=ceil(cars_t*(1+7/100));
years+=1;
}
printf("Xronia:%ld\n",years);
printf("Arithmos Autokinhton:%ld\n",cars_t);
}这只是一个带有while函数的极其简单的程序。但是由于某些原因,它根本没有给出任何输出。然而,我注意到的是,只要我删除while函数(以及其中的代码),程序就会运行得很好。有人能告诉我怎么解决这个问题吗?提前谢谢。
发布于 2021-11-01 14:59:57
这是因为您已将cars_t声明为整数(长整型)值,7/100也是整数,因此计算结果为零。因此,当cars_t不增加时,您会陷入循环中。
相反,您希望cars为浮点值,并强制将7/100计算为浮点值:
#include <stdio.h>
#include <tgmath.h>
long main(){
long years;
double cars_t;
cars_t=80000;
years=0;
while (cars_t<=160000){
cars_t=ceil(cars_t*(1+7.0/100));
years+=1;
}
printf("Xronia:%ld\n",years);
printf("Arithmos Autokinhton:%lf\n",cars_t);
}https://stackoverflow.com/questions/69798672
复制相似问题