2016年7月2日 星期六

Q10062: Tell me the frequencies!


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1003

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  int f = 0;
  while (scn.hasNext()) {
   int ary[] = new int[95], max = 0;
   if (f != 0)
    System.out.println();
   f++;
   String str = scn.nextLine();
   char arr[] = str.toCharArray();
   for (int i = 0; i < arr.length; i++) {

    ary[arr[i] - ' ']++;
    if (ary[arr[i] - ' '] > max)
     max = ary[arr[i] - ' '];

   }
   for (int i = 1; i <= max; i++)
    for (int j = ary.length - 1; j >= 0; j--)
     if (ary[j] == i)
      System.out.printf("%d %d\n", j + ' ', ary[j]);

  }

 }
 /* 
    題目:Q10062: Tell me the frequencies!
    作者:1010
    時間:西元 2016 年 7 月 */

}


困難度 
這題要注意的地方是換行位置每筆測資尾都要換行,除了最後一筆測資
還有要注意是每一個字母都要計數(包含符號空白ASCii 32~126)
當然要設128個陣列也行浪費記憶體空間而已並且浪費一點點時間

沒有留言:

張貼留言