English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

Use C or other programming language to design a program satisfying the following requirements:
(1)Input: a string consisting of the English lowercase alphabet.
(2)Output: the frequency of each letter in the string.
(3)Example: if input the string \"book\" ,than the following message will be shown:
b=1,k=1,o=2.

2006-07-17 16:55:56 · 1 個解答 · 發問者 大C 1 in 電腦與網際網路 程式設計

我希望的答案是百分之百正確的
要完全合題意
^^辛苦囉

2006-07-17 18:19:09 · update #1

答案也是要完全正確無誤的喔

2006-07-17 18:19:48 · update #2

最後面o印出來不會等於2耶 會等於1 是哪裡又問題呢?

2006-07-18 16:50:44 · update #3

1 個解答

#include

int f[26];

void main()
{ int i;
char t[256];

scanf("%s", t);
for (i=strlen(t)-1; i>=0; i--) f[t[i]-'a']++;

for (i=0; i<26; i++)
if (f[i]) printf("%c = %d, ", i+'a', f[i]);
}

這程式基本上是對的。
我設定在它要簡短,所以沒有加入任何的檢查(如:輸入的不是小寫字母)。
除了上述問題外,最後一個印出的後面是 , 而不是 . 是這程式唯一不合題意的地方。
要是您要 100% 合題,外加錯誤檢查,請說明,我會再補上。

2006-07-17 22:42:40 補充:
#include #include int c, e, f[26];int main(){ int i; char t[256]; scanf("%s", t); for (i=strlen(t)-1; i>=0; i--) { if (t[i]<'a' || t[i]>'z') { printf("Illegal input\n"); return 1; } if (!f[t[i]-'a']++) c++; }

2006-07-17 22:43:40 補充:
for (i=0; i<26; i++) if (f[i]) { c--; printf(c?"%c = %d, ":"%c = %d.\n", i+'a', f[i]); } return 0;}ok, Done!

2006-07-17 22:47:16 補充:
喔,對了。那個 int c, e, f[26];只要: int c, f[26];就可以了。 e 是我測試用的,程式寫好不用了,忘了刪去。

2006-07-18 00:16:28 補充:
{ c--;printf(c?"%c = %d, ":"%c = %d.\n", i+'a', f[i]);}可以改成printf(--c?"%c = %d, ":"%c = %d.\n", i+'a', f[i]);

2006-07-19 12:20:01 補充:
我剛在我的電腦又試過了,沒錯喔!而且還再從這裡 copy 回我的電腦再試!還是沒錯喔!不管是第二次補充的,還是去 e 的、把 c-- 並到下一列變成 --c 答案都是對的。book 會印出 o = 2.calculation, testabcdefg, 等都試過了,沒問題!您自己對一下,應該是您 copy 時有錯喔!

2006-07-17 18:16:28 · answer #1 · answered by ? 7 · 0 0

fedest.com, questions and answers