void countdigst(int last_number, int *count_arry) {
for (int i = 1; i <= last_number; i++) {
int temp = 0;
while (i != 0) { // 內層修改了外層 for 的計數器 i
temp = i % 10;
count_arry[temp]++;
i /= 10; // i 變 0,導致 for 的 i++ 後仍為 1 → 死循環
}
}
}
`for` 迴圈的計數器 `i` 在 `while` 內被歸零,`for` 的 `i++` 使其變回 1,陷入無限循環 `1 → 0 → 1 → 0...`。
void countdigst(int last_number, int *count_arry) {
for (int i = 1; i <= last_number; i++) {
int n = i; // 複製一份用於分解位數
while (n != 0) {
int temp = n % 10;
count_arry[temp]++;
n /= 10;
}
}
}
若 `last_number` 含 0(如 10、100),`while(n!=0)` 會跳過 0 的統計。需特判:
if (n == 0) count_arry[0]++; // 數字 0 本身有一位 0
else {
while (n != 0) { ... }
}