我是靠谱客的博主 无心小蚂蚁,最近开发中收集的这篇文章主要介绍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);
        }
    }
}

最后

以上就是无心小蚂蚁为你收集整理的PAT1028排序总结的全部内容,希望文章能够帮你解决PAT1028排序总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部