#43717: java參考答案


w122322222@gmail.com (m n j h p)

學校 : 不指定學校
編號 : 289597
來源 : [140.136.20.201]
最後登入時間 :
2024-10-27 20:06:50
c012. 10062 - Tell me the frequencies! -- UVa10062 | From: [140.136.20.201] | 發表日期 : 2024-10-24 13:09

import java.util.*;//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
class word implements Comparable<word>{
int asii;
int count;
word(int asii,int count){
this.asii=asii;
this.count=count;
}
public int compareTo(word other){
if(this.count==other.count){
return other.asii-this.asii;
}
return this.count-other.count;
}
}
public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
ArrayList<word>list=new ArrayList<>();
String in=sc.nextLine();
for(int i=0;i<in.length();i++){
int o=0;
for(int k=0;k<list.size();k++){
if((int)in.charAt(i)==list.get(k).asii){
list.get(k).count+=1;
o=1;
break;
}
}
if(o==0){
list.add(new word((int)in.charAt(i),1));
}
}
Collections.sort(list);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).asii+" "+list.get(i).count);
}
System.out.println();

}

//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.

}
}
 
ZeroJudge Forum