网站建设资讯

NEWS

网站建设资讯

java成绩排序

题目内容:
输入
N组数据
名字+成绩

创新互联建站主营掇刀网站建设的网络公司,主营网站建设方案,重庆APP开发公司,掇刀h5微信小程序定制开发搭建,掇刀网站营销推广欢迎掇刀等地区企业咨询

输出:按照成绩升序或降序排列,若成绩相同,按照录入的先后顺序

import java.util.Scanner;

public class Score {

        private static String[] names;
        private static int[] grades;
        private static boolean compare(int i, int j, int flag){
            return flag == 0 ? grades[i] > grades[j] : grades[i] < grades[j];
        }
        private static void exch(int i, int j){

            String tempS = names[j];
            int tempG = grades[j];
            names[j] = names[i];
            grades[j] = grades[i];
            names[i] = tempS;
            grades[i] = tempG;

        }
        public static void sort(int flag){
            for(int i = 1; i < names.length; i++)
                for(int j = i; j > 0 && compare(j,j - 1,flag); j--){
                    exch(j,j - 1);
                }
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
            while(input.hasNext()){
                int n = input.nextInt();//人数
                int flag = input.nextInt();
                names = new String[n];
                grades = new int[n];
                for(int i = 0; i < n; i++){
                    names[i] = input.next();
                    grades[i] = input.nextInt();
                }
                sort(flag);
                for(int i = 0; i < n; i++){
                    System.out.println(names[i] + " " + grades[i]);
                }
            }
        }

}

新闻标题:java成绩排序
分享网址:http://cdweb.net/article/jipopc.html