java成绩排序-创新互联
题目内容:
输入
N组数据
名字+成绩
输出:按照成绩升序或降序排列,若成绩相同,按照录入的先后顺序
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]);
}
}
}
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
本文题目:java成绩排序-创新互联
转载来源:http://azwzsj.com/article/ccgjhs.html