OK所以這個代碼的目的是提示用戶一個整數,然後 該程序將打印出每個數字0-9和它在用戶提供的號碼的出現次數。 輸出應該是這個樣子卡在這個C代碼
Enter a number: 28666
0 is repeated 0 times.
1 is repeated 0 times.
2 is repeated 1 times.
3 is repeated 0 times.
4 is repeated 0 times.
5 is repeated 0 times.
6 is repeated 3 times.
7 is repeated 0 times.
8 is repeated 1 times.
9 is repeated 0 times.
這裏是我的代碼和輸出至今。
#include <stdbool.h>
#include <stdio.h>
int main(void)
{
bool digit_seen[10] = {false};
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
while (n > 0) {
digit = n % 10;
if (digit_seen[digit])
break;
digit_seen[digit] = true;
n /= 10;
}
if (n > 0)
for(int i=0; i<digit; i++)
digit_seen[i]++;
for(int i=0; i<digit; i++)
printf(" %d is occur %d times \n",i,digit_seen[i]);
if (n < 0)
printf("No repeated digit\n");
return 0;
}
這裏是我的輸出
Enter a number: 147795655
0 is occur 1 times
1 is occur 1 times
2 is occur 1 times
3 is occur 1 times
4 is occur 1 times
,你可以看到我沒有收到輸出,我需要和我不明白why.If有人可以幫助我理解我要去哪裏錯誤,這將是一個很大的幫助,謝謝。
'bool digit_seen [10] = {false};' - >'int digit_seen [10] = {0};'。記住:你正在計算出現次數,就像在'digit_seen [i] ++;'中一樣。 –
負數不允許重複數字嗎?爲什麼最後一個'if(...)'用於? –