1004 成绩排名 (20 分) Java练习&PTA乙级 class代替结构体(&排序)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiang_6/article/details/89318571

1. C++中结构体很好用,java中要在Main函数外面定义class,继承Comparable用于排序

2. string 直接读取一行,由空格分割


import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


class node implements Comparable<node> {
	String name = null;
	String t = null;
	int sco = 0;
	
	public node(String name, String t, int sco) {
		this.name = name;
		this.t = t;
		this.sco = sco;
	}
	
	@Override
	public int compareTo(node o) {
		// TODO Auto-generated method stub
		return (o.sco - this.sco);
	}
}

public class Main {
	
	public static int maxn = 1000 + 7;
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt(); 
		node[] a = new node[maxn];
		in.nextLine();
		for(int i = 1; i <= n; ++i) {
			String tmp = in.nextLine();
			String[] stu = tmp.split(" ");
			int t = Integer.valueOf(stu[2]).intValue();
			a[i] = new node(stu[0], stu[1], t);
		}
		
		Arrays.sort(a, 1, 1+n);
		
//		for(int i = 1; i <= n; ++i) {
//			System.out.println(a[i].name + " " + a[i].t + " " + a[i].sco);
//		}
		System.out.println(a[1].name + " " + a[1].t);
		System.out.println(a[n].name + " " + a[n].t);
	}
	
}

猜你喜欢

转载自blog.csdn.net/xiang_6/article/details/89318571