Board logo

標題: 404 字串與檔案處理 (字母出現次數) [打印本頁]

作者: 陳育霖    時間: 2023-10-6 22:41     標題: 404 字串與檔案處理 (字母出現次數)

1. 題目說明:
請依下列題意進行作答,使輸出值符合題意要求。

2. 設計說明:
請撰寫一程式,讓使用者輸入一個長度不超過50字元的字串,此字串均為小寫字母,輸出該字串出現最多次的英文字母以及出現的次數。

提示:假設出現過最多次英文字母的次數唯一。
提示:若使用 Java 語言答題,請以「JP」開頭命名包含 main 靜態方法的 class,評測系統才能正確評分。

3. 輸入輸出:
輸入說明
一個長度不超過50字元的字串,此字串均為小寫字母

輸出說明
該字串出現最多次的英文字母以及出現的次數

範例輸入
refrigerator
範例輸出
r
4


本帖隱藏的內容需要回復才可以瀏覽

作者: 曾宥程    時間: 2023-10-7 11:46

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int num[26]={0};
  6.     string str;
  7.     cin>>str;
  8.     int index=0;
  9.     for(int i=0 ; i<str.length() ; i++)
  10.     {
  11.         num[str[i]-'a']++;
  12.         if(num[str[i]-'a'] > num[index])
  13.         {
  14.             index=str[i]-'a';
  15.         }
  16.     }
  17.     cout << char(index+'a') << endl << num[index] << endl;
  18. }
複製代碼

作者: 王銘鴻    時間: 2023-10-20 22:00

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int num[26] = {0};
  6.     string str;
  7.     cin >> str;
  8.     for(int i=0; i <str.size(); i++)
  9.     {
  10.         num[str[i] - 'a']++;
  11.     }
  12.     int index =0;
  13.     for(int i = 1; i< 26; i++)
  14.     {
  15.         if(num[i] > num[index])
  16.         {
  17.             index = i;
  18.         }
  19.     }
  20.     cout << char(index + 'a') << endl <<num[index];
  21.     return 0;
  22. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2