![](/img/trans.png)
[英]Program loops infinitely if a non-integer is entered, and does not accept plural digit inputs
[英]Loops running infinitely for big inputs
我是自學者,我正在研究人口增長問題,當我輸入一個我想要達到的大結束數字時,我遇到了循環無限運行的問題。
#include <cs50.h>
#include <stdio.h>
int main(void) {
// TODO: Prompt for start size
int n;
do {
n = get_int("Enter the starting size of your llamas: ");
} while (n <= 8);
// TODO: Prompt for end size
int j;
do {
j = get_int("Enter the Ending size of your llamas: ");
} while (j <= n);
// TODO: Calculate number of years until we reach threshold
int k = 0;
do {
n = n + (n/3) - (n/4);
printf("The number is %i\n", n);
k++;
} while (n != j);
// TODO: Print number of years
printf("The number is %i\n", k);
}
答案應該是達到最終大小的美洲駝所需的年數,但我無法輸入大量的最終大小,你能幫我找出問題所在,也許在我的數學或符號中. 提前致謝。
對於大數, n
在每次迭代中增加超過1
,因此它有可能變得大於j
而不是首先等於它。
更改測試while(n;= j);
到while(n < j);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.