成绩排序

问题描述
  给出n个学生的成绩,将这些学生按成绩排序,
  排序规则,优先考虑数学成绩,高的在前;数学相同,英语高的在前;数学英语都相同,语文高的在前;三门都相同,学号小的在前
输入格式
  第一行一个正整数n,表示学生人数
  接下来n行每行3个0~100的整数,第i行表示学号为i的学生的数学、英语、语文成绩
输出格式
  输出n行,每行表示一个学生的数学成绩、英语成绩、语文成绩、学号
  按排序后的顺序输出
样例输入
2
1 2 3
2 3 4
样例输出
2 3 4 2
1 2 3 1
import java.util.Scanner;


public class Main {
    public static class score {
        int sx;
        int yy;
        int yw;
        int Num;
    }
    static score []a;
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            a=new score [n];
            for (int i = 0; i < n; i++) 
            {    
                score b =new score();
                b.sx=sc.nextInt();
                b.yy=sc.nextInt();
                b.yw=sc.nextInt();
                b.Num=i+1;
                a[i]=b;
            }
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (a[i].sx < a[j].sx)
                    {
                        score s = a[i];//交换
                        a[i] = a[j];
                        a[j] = s;
                    }
                    else if (a[i].sx == a[j].sx)
                    {
                        if (a[i].yy < a[j].yy)
                        {
                            score s = a[i];//又打一遍
                            a[i] = a[j];
                            a[j] = s;
                        }
                        else if (a[i].yy == a[j].yy)
                        {
                            if (a[i].yw < a[j].yw)
                            {
                                score s = a[i];//重复
                                a[i] = a[j];
                                a[j] = s;
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                System.out.println(a[i].sx+" "+a[i].yy+" "+a[i].yw+" "+a[i].Num);
            }

        }
}

 

posted @ 2020-02-26 14:18  智阿广  阅读(334)  评论(0)    收藏  举报