主要思路:
创建两个一维数组对应学号和成绩,然后冒泡排序同时维护两个数组内值,在应用层实现同时替换(不会list集合QAQ)。然后使用双指针法设置left,right两个变量形成滑动窗口,找到一个区间的值来进行学号的排序。学号的排序只要确定同分的区间就很容易的调用Arrays.sort(int[] arr,int left,int right)方法实现。
package demo.day0203;
import java.util.Arrays;
import java.util.Scanner;
//class Student{
// public int xh;
// public int cj;
// public Student(int xh,int cj){
// this.xh=xh;
// this.cj=cj;
// }
//}
public class StudentSort {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[] xh=new int[N];
int[] cj=new int[N];
for(int i=0;i<N;i++){
xh[i]=sc.nextInt();
cj[i]=sc.nextInt();
}
for(int i=0;i<N-1;i++){
for(int j=0;j<N-1-i;j++){
if(cj[j]>cj[j+1]){
int temp=cj[j];
cj[j]=cj[j+1];
cj[j+1]=temp;
temp=xh[j];
xh[j]=xh[j+1];
xh[j+1]=temp;
}
}
}
int left=0,right=0;
while(right<N) {
while (right < N && cj[left] == cj[right]) right++;
Arrays.sort(xh, left, right );
left = right;
}
for(int i=0;i<N;i++){
System.out.println(xh[i]+" "+cj[i]);
}
}
}

京公网安备 11010502036488号