這是我完成的代碼,但由於某些原因,我仍然無法弄清楚原因。它會正確顯示第0年的適當人口,但第1年,它將取上一年的人口數值,並乘以該比率,而不是使用我在代碼中使用的所提供的公式 公式爲NextYr = Rate * CurrentYr *(1-(當前年/ 1000000))C程序 - 每年計算和顯示使用提供的方程的人口
任何幫助將不勝感激!
例如,
Enter the initial egret population: 12
Enter the rate: 2
Year Population
0 12
1 24
2 48
3 96
and so on
#include<stdio.h>
int main()
{
int count=1,year_num;
float Rate;
unsigned long CurrentYr;
unsigned long NextYr;
while (count<=1)
{
printf("Enter the initial egret population: ");
scanf("%d",&CurrentYr);
printf("Enter the rate: ");
scanf("%f",&Rate);
printf("Year Population\n");
printf("---- ----------\n");
if ((CurrentYr>0 && CurrentYr<1000000) && (Rate>0 && Rate<4))
{
NextYr = CurrentYr;
for(year_num=0;year_num<=25;year_num++)
{
NextYr = Rate * NextYr * (1-NextYr/1000000);
printf("%4d%12d\n",year_num,NextYr);
}
break;
}
else if ((CurrentYr < 0 || CurrentYr > 1000000) || (Rate<0 || Rate>4))
{
printf("Invalid Input!");
printf("Enter the initial egret population: ");
scanf("%d",&CurrentYr);
printf("Enter the rate: ");
scanf("%f",&Rate);
if ((CurrentYr>0 && CurrentYr<1000000) && (Rate>0 && Rate<4))
{
NextYr = CurrentYr;
for(year_num=0;year_num<=25;year_num++)
{
printf("%4d%12d\n",year_num,NextYr);
NextYr = Rate * NextYr * (1-NextYr/1000000);
}
break;
}
else
{
printf("No more chance ! Bye ! ");
}
}
return 0;
}
}
NextYr/1000000是整數除法(收率0,如果在NextYr [0,1000000))。 – SleuthEye