小鑫の日常系列故事(十)——排名次-2741
阿新 • • 發佈:2018-11-02
原題連結
Java語言沒有c/c++中的struct,沒辦法還是得用Java來寫,只能從其他的地方借鑑了,其中,類是一個不錯的選擇,雖然還不會,
但其實可以理解為一個函式,內部包含實現的功能。
import java.util.Arrays; import java.util.Scanner; class S implements Comparable<S> { int t; String s; public S(String s,int t) { this.s=s; this.t=t; } public int compareTo(S a) { if(this.t-a.t>0) return this.t-a.t; //else return a.t-this.t; else return this.t-a.t; } } public class Main { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int i,n,t; n = reader.nextInt(); String s; S d[] = new S[55]; for(i=0;i<n;i++) { s = reader.next(); t = reader.nextInt(); d[i] = new S(s,t); } Arrays.sort(d,0,n); for(i=n-1;i>=0;i--) { System.out.println(d[i].s+" "+d[i].t); } reader.close(); } }