前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >试题 算法提高 成绩排名

试题 算法提高 成绩排名

作者头像
SingYi
发布2022-07-13 18:27:54
2690
发布2022-07-13 18:27:54
举报
文章被收录于专栏:Lan小站

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

  小明刚经过了一次数学考试,老师由于忙碌忘记排名了,于是老师把这个光荣的任务交给了小明,小明则找到了聪明的你,希望你能帮他解决这个问题。

输入格式

  第一行包含一个正整数N,表示有个人参加了考试。接下来N行,每行有一个字符串和一个正整数,分别表示人名和对应的成绩,用一个空格分隔。

输出格式

  输出一共有N行,每行一个字符串,第i行的字符串表示成绩从高到低排在第i位的人的名字,若分数一样则按人名的字典序顺序从小到大。

样例输入

3 aaa 47 bbb 90 ccc 70

样例输出

bbb ccc aaa 【数据规模和约定】 人数<=100,分数<=100,人名仅包含小写字母。

代码语言:javascript
复制
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);
		}
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档