我已经看过了,想知道我的数学问题在哪里。我相信它应该正确计算,但浮点数不会四舍五入,0.75 到 1 以增加出生/死亡人数。我是c的新手。这是我到目前为止的代码:
float births(long popul);
float deaths(long pop);
long yearAdjustment(long pop);
int threshold(long population, long end);
int main(void){
long begin = 0;
long end = 0;
int year = 0;
float input = 0.0;
do{
// TODO: Prompt for start size
input = get_float("Beginning population: ");
begin = (long) roundf(input);
} while (begin < 9);
do{
// TODO: Prompt for end size
input = get_float("Ending population: ");
end = (long) roundf(input);
} while (end < begin || end <= 0);
if(begin == end)
{
year = 0;
} else
{
year = threshold(begin, end);
}
// TODO: Print number of years
printf("Years: %i\n", year);
}
float births(long pop){
float tmp = pop / 3;
return tmp;
}
float deaths(long pop){
float tmp = pop / 4;
return tmp;
}
long yearAdjustment(long pop){
long tmp = pop + ((long) roundf(births(pop) - deaths(pop)));
return tmp;
}
int threshold(long population, long end){
int years = 0;
long tmp = 0;
// TODO: Calculate number of years until we reach threshold
while (tmp < end){
tmp += yearAdjustment(population);
years++;
}
return years;
}
我使用的是多头,因为数字可能从数千开始。在出生/死亡的划分中,花车是为了更精确,更圆润。本质上,它应该分别增加大约 1/10/100... 的单个/数十/数百 ... 输入。输入 9 时为 1.25。这就是小数点很重要的地方。从技术上讲,每 4 年我会额外获得 1 次。说18结束应该是8年。
谢谢你。