SDUT-1525 字元統計2(JAVA*)__JAVA

來源:互聯網
上載者:User
字元統計2 Time Limit: 1000 ms  Memory Limit: 65536 KiB Submit  Statistic Problem Description

輸入英文句子,輸出該句子中除了空格外出現次數最多的字元及其出現的次數。 Input

輸入資料包含多個測試執行個體,每個測試執行個體是一個長度不超過100的英文句子,佔一行。 Output

逐行輸出每個句子中出現次數最多的字元及其出現的次數(如果有多個字元的次數相同,只輸出ASCII碼最小的字元)。 Sample Input

I am a studenta good programming problemABCD abcd ABCD abcd
Sample Output
a 2o 4A 2
Hint   Source

java數組初始化方法:java.util.Arrays.fill(數組名,數值)

參考:https://blog.csdn.net/alenc/article/details/1500113

import java.util.*;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);char c[] = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };while (input.hasNext()) {char arr[] = input.nextLine().toCharArray();int num[] = new int[52];Arrays.fill(num, 0);//Arrays.fill() 方法只適合於把數組用同一個值初始化。int sum = 0, t = 0;for (int i = 0; i < arr.length; i++) {for (int j = 0; j < 52; j++) {if (c[j] == arr[i]) {num[j]++;if (num[j] > sum) {sum = num[j];t = j;}if (num[j] == sum) {if (c[t] > c[j])t = j;}break;}}}System.out.printf("%c %d\n", c[t], sum);}}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.