小鑫の日常系列故事(十)——排名次-2741

版权声明:转载请注明出处 https://blog.csdn.net/weixin_42981803/article/details/82659039

原题链接

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();

	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42981803/article/details/82659039
今日推荐