首页 > 试题广场 >

成绩排序

[编程题]成绩排序
  • 热度指数:45583 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
用一维数组存储学号和成绩,然后,按成绩排序输出。

输入描述:
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。


输出描述:
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
示例1

输入

3
1 90
2 87
3 92

输出

2 87
1 90
3 92
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()) {
			int n = scanner.nextInt();
			Map<Integer, Integer> myMap = new TreeMap<Integer, Integer>();
			for (int i = 0; i < n; i++) {
				int sno = scanner.nextInt();
				int sgrade = scanner.nextInt();
				myMap.put(sno, sgrade);
			}
			
			//使用list对entrySet排序
			List<Map.Entry<Integer, Integer>> myList = new ArrayList<>(myMap.entrySet());
			myList.sort(Map.Entry.comparingByValue());
			
			//创建一个LinkedHashMap,按照排序后的顺序插入entry
			Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>();
			for(Map.Entry<Integer, Integer> entry : myList) {
				sortedMap.put(entry.getKey(), entry.getValue());
			}
			
			
			for(Map.Entry<Integer, Integer> entry:sortedMap.entrySet()) {
				System.out.println(entry.getKey()+" "+entry.getValue());
			}

		}
	}
}

编辑于 2024-03-18 15:38:58 回复(0)
import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = Integer.parseInt(in.nextLine());
            String[] inputs = new String[n];
            for(int i=0;i<n;i++){
                String[] item = in.nextLine().split(" ");
                if(Integer.parseInt(item[0])<10){
                    //因为n<=100,补零为了后面排序
                    item[0] = "0"+item[0];
                }
                inputs[i] = item[1]+" "+item[0];
            }
            //排序
            Arrays.sort(inputs);
            for(int i=0;i<n;i++){
               String[] item = inputs[i].split(" ");
               if(Integer.parseInt(item[1])<10){
                   //去掉前面补的零
                   item[1] = item[1].substring(1,2);
               }
               String temp = item[1]+" "+item[0];
               System.out.println(temp);
            }
            
        }
    
    }
}

发表于 2023-03-10 20:13:39 回复(0)
import java.util.*;
public class  Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[n][2];
        for(int i = 0; i < n; i++){
            arr[i][0] = sc.nextInt();
            arr[i][1] = sc.nextInt();
        }
        Arrays.sort(arr, new Comparator<int[]>(){
            public int compare(int[] a, int[] b){
                if(a[1] == b[1]){
                    return a[0] - b[0];
                }else{
                    return a[1] - b[1];
                }
            }
        });
        for(int[] i : arr){
            System.out.println(i[0] + " " + i[1]);
        }
    }
}
发表于 2020-12-31 20:30:41 回复(0)
Java 解法一,使用面向对象的写法,清晰
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < n; i++) list.add(new Student(scanner.nextInt(),scanner.nextInt()));
        Collections.sort(list);
        for (Student student : list) System.out.println(student);
    }
    
    public static class Student implements Comparable<Student>{
        Integer id;
        Integer score;

        public Student(Integer id,Integer score){
            this.id=id;
            this.score=score;
        }
        @Override
        public int compareTo(Student o) {
           return this.score.equals(o.score) ?id.compareTo(o.id):score.compareTo(o.score);
        }
        @Override
        public String toString() {
            return id+" "+score;
        }
    }
}
解法二:
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) list.add(scanner.nextLine());
        list.sort((o1, o2) -> {
            String[] s1 = o1.split(" ");
            Integer id1 = Integer.parseInt(s1[0]);
            Integer score1= Integer.parseInt(s1[1]);
            String[] s2 = o2.split(" ");
            Integer score2= Integer.parseInt(s2[1]);
            Integer id2 = Integer.parseInt(s2[0]);
            return score1.equals(score2) ?id1.compareTo(id2):score1.compareTo(score2);
        });
        for (String s : list) System.out.println(s);
    }
}



编辑于 2020-03-17 16:45:03 回复(1)
public class Main {
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Student[] students = new Student[n];
        for(int i=0;i<n;i++){
            int number = scanner.nextInt();
            int grade = scanner.nextInt();
            students[i] = new Student(grade, number);
        }
        Arrays.stream(students)
                .sorted((o1, o2) -> o1.grade == o2.grade ? o1.number - o2.number : o1.grade - o2.grade)
                .forEach(System.out::println);
    }
}
class Student{
    int grade;
    int number;
    public Student(int grade, int number) {
        this.grade = grade;
        this.number = number;
    }
    @Override
    public String toString() {
        return number + " " + grade;
    }
}

发表于 2020-03-04 15:18:17 回复(1)

Student类中几个set方法never used,可删掉,写的时候都写了,习惯。

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Student {
    private int id;
    private int grade;

    public Student(int id, int grade) {
        this.id = id;
        this.grade = grade;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getGrade() {
        return grade;
    }

    public String toString() {
        return id + " " + grade;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            int n = scan.nextInt();
            Student[] arr = new Student[n];
            for (int i = 0; i < arr.length; i++) {
                int id = scan.nextInt();
                int grade = scan.nextInt();
                arr[i] = new Student(id, grade);
            }
            Arrays.sort(arr, new MyComparator());
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i].toString());
            }
        }
    }

    private static class MyComparator implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            if (s1.getGrade() != s2.getGrade()) {
                return s1.getGrade() > s2.getGrade() ? 1 : -1;
            } else {
                return s1.getId() > s2.getId() ? 1 : -1;
            }
        }
    }
}
编辑于 2018-05-28 19:12:15 回复(0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Comparator;

class Info {
    private int ordinal;
    private int grade;
    public Info(int p,int q){
        ordinal = p;
        grade = q;
    }
    public Info(){};
    public int getOrdinal(){
        return ordinal;
    }
    public void setOrdinal(int p){
        ordinal = p;
    }
    public int getGrade(){
        return grade;
    }
    public void setGrade(int q){
        grade = q;
    }

}

class SortByGrade implements Comparator<Object> {
    public int compare(Object o1,Object o2){
        Info s1 = (Info) o1;
        Info s2 = (Info) o2;  

        if(s1.getGrade() != s2.getGrade()){
            return s1.getGrade() > s2.getGrade() ? 1:-1;
        }else{
            return s1.getOrdinal()> s2.getOrdinal() ? 1:-1;
        }
}
}

public class Main {

    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Info[] stus = new Info[N];
        for(int t=0;t<N;t++){
            stus[t] = new Info();
        }
        
        for(int i = 0 ; i < N; i++){
            stus[i].setOrdinal(sc.nextInt());
            stus[i].setGrade(sc.nextInt());
        }

        List<Info> stList = new ArrayList<Info>();
        for( int t=0;t<N;t++){
            stList.add(stus[t]);
        }
        Collections.sort(stList, new SortByGrade());        
        for(Info out:stList){
            System.out.println(out.getOrdinal()+" "+out.getGrade());
        }
    }
}

发表于 2018-03-26 19:35:56 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

/**
 * Created by fhqplzj on 17-1-26 at 下午1:08.
 */
public class My4 {
    static class Student implements Comparable<Student> {
        private int index;
        private int score;

        Student(int index, int score) {
            this.index = index;
            this.score = score;
        }

        @Override
        public String toString() {
            return String.format("%d %d", index, score);
        }

        @Override
        public int compareTo(Student o) {
            if (score != o.score) {
                return score < o.score ? -1 : 1;
            } else {
                return new Integer(index).compareTo(o.index);
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> students = new ArrayList<>();
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                students.add(new Student(scanner.nextInt(), scanner.nextInt()));
            }
            Collections.sort(students);
            for (Student student : students) {
                System.out.println(student);
            }
        }
    }
}

发表于 2017-01-26 13:18:43 回复(0)