试题 算法提高 成绩排名

Lan
Lan
2020-03-28 / 0 评论 / 1,086 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年03月28日,已超过1461天没有更新,若内容或图片失效,请留言反馈。
资源限制
时间限制:1.0s   内存限制:256.0MB
问题描述
  小明刚经过了一次数学考试,老师由于忙碌忘记排名了,于是老师把这个光荣的任务交给了小明,小明则找到了聪明的你,希望你能帮他解决这个问题。
输入格式
  第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。
输出格式
  输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。
样例输入
3
aaa 47
bbb 90
ccc 70
样例输出
bbb
ccc
aaa 【数据规模和约定】
人数<=100,分数<=100,人名仅包含小写字母。
import java.util.*;

public class 成绩排名 {
	public static class student{
		public String name;
		public int grade;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		student[] stu = new student[n];
		for (int i = 0; i < stu.length; i++) {
			stu[i] = new student();
			stu[i].name = input.next();
			stu[i].grade = input.nextInt();
		}
		for (int i = 0; i < stu.length-1; i++) {
			for (int j = 0; j < stu.length-1-i; j++) {
				if (stu[j].grade<stu[j+1].grade) {
					student temp = stu[j];
					stu[j] = stu[j+1];
					stu[j+1] = temp;
				}else if (stu[j].grade==stu[j+1].grade) {
					int k =0;
					while (stu[j].name.charAt(k)==stu[j+1].name.charAt(k)) {
						k++;
						if (stu[j].name.length()==k) {
							k--;
							break;
						}
					}
					if (stu[j].name.charAt(k)>stu[j+1].name.charAt(k)) {
						student temp = stu[j];
						stu[j] = stu[j+1];
						stu[j+1] = temp;
					}
				}
			}
		}
		for (int i = 0; i < stu.length; i++) {
			System.out.println(stu[i].name);
		}
	}

}


0

评论 (0)

取消