PAT1028排序总结

PAT1028常用解题方法,实现结构体
1.常用的输入输出方法:

		Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int [] m = new int[n];
        for(int i = 0;i<n ; i++){
            m[i ]= scan.nextInt();
        }
        //适用于一个n,剩下均为整数

2.利用IO

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        String[] s=br.readLine().split(" ");
        int[] m=new int[n];
        for(int i=0;i<n;i++){
            m[i]=Integer.parseInt(s[i]);
        }
        //函数体注意 throws Exception

3.其中2也可以解决有字符串的情况
还可以全程使用nextInt(), next();

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), c = sc.nextInt();
ArrayList<Stu> list = new ArrayList<Stu>();
for (int i = 0; i < n; i++) {
	list.add(new Stu(sc.next(), sc.next(), sc.nextInt(), c));
}

class Stu implements Comparable<Stu> {
	String ID;
	String name;
	int score;
	int column;
 
	public Stu(String iD, String name, int score, int column) {
		super();
		ID = iD;
		this.name = name;
		this.score = score;
		this.column=column;
	}
}

最后提供解法:

import java.util.*;
class Student implements Comparable<Student>{
    String id;
    String name;
    int score;
    int column;
    public Student(String id, String name, int score, int column) {
        this.name = name;
        this.column = column;
        this.id = id;
        this.score = score;
    }
    @Override
    public int compareTo(Student s) {
        if(column == 1) {
            return this.id.compareTo(s.id);
        }else if(column == 2) {
            if(name.equals(s.name)) return  this.id.compareTo(s.id);
            else return name.compareTo(s.name);
        }else {
            if(score == s.score) return id.compareTo(s.id);
            else return score - s.score;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), c = sc.nextInt();
        ArrayList<Student> list = new ArrayList<>();
        for(int i = 0; i< n; i++) {
            list.add(new Student(sc.next(), sc.next(), sc.nextInt(), c));
        }
        Collections.sort(list);
        for(Student stu: list) {
            System.out.println(stu.id + " "+ stu.name + " " + stu.score);
        }
    }
}

利用接口也可实现

package newOne;

import java.util.*;

class Student{
    String id;
    String name;
    int score;

    public Student(String id, String name, int score) {
        this.name = name;
        this.id = id;
        this.score = score;
    }
}


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), column = sc.nextInt();
        ArrayList<Student> list = new ArrayList<>();
        for(int i = 0; i< n; i++) {
            list.add(new Student(sc.next(), sc.next(), sc.nextInt()));
        }
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student a, Student b) {
                if(column == 1) {
                    return a.id.compareTo(b.id);
                }else if(column == 2) {
                    if(a.name.equals(b.name)) return  a.id.compareTo(b.id);
                    else return a.name.compareTo(b.name);
                }else {
                    if(a.score == b.score) return a.id.compareTo(b.id);
                    else return a.score - b.score;
                }
            }
        });
        for(Student stu: list) {
            System.out.println(stu.id + " "+ stu.name + " " + stu.score);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值