L2-027 名人堂与代金券 - java

L2-027 名人堂与代金券


时间限制
150 ms
内存限制
64 MB


题目描述:

对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,总评成绩必须达到 60 分及以上,并且有另加福利:总评分在 [G, 100] 区间内者,可以得到 50 元 PAT 代金券;在 [60, G) 区间内者,可以得到 20 元PAT代金券。全国考点通用,一年有效。同时任课老师还会把总评成绩前 K 名的学生列入课程“名人堂”。本题就请你编写程序,帮助老师列出名人堂的学生,并统计一共发出了面值多少元的 PAT 代金券。

输入格式:
输入在第一行给出 3 个整数,分别是 N(不超过 10 000 的正整数,为学生总数)、G(在 (60,100) 区间内的整数,为题面中描述的代金券等级分界线)、K(不超过 100 且不超过 N 的正整数,为进入名人堂的最低名次)。接下来 N 行,每行给出一位学生的账号(长度不超过15位、不带空格的字符串)和总评成绩(区间 [0, 100] 内的整数),其间以空格分隔。题目保证没有重复的账号。

输出格式:
首先在一行中输出发出的 PAT 代金券的总面值。然后按总评成绩非升序输出进入名人堂的学生的名次、账号和成绩,其间以 1 个空格分隔。需要注意的是:成绩相同的学生享有并列的排名,排名并列时,按账号的字母序升序输出。

输入样例:
10 80 5
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 65
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 80
jack@ucla.edu 88
bob@cmu.edu 80
ken@163.com 70
输出样例:
360
1 uh-oh@163.com 96
2 jack@ucla.edu 88
3 anyone@qq.com 87
3 cy@pat-edu.com 87
5 bob@cmu.edu 80
5 zoe@mit.edu 80


给定n个人的邮箱以及分数

求一共发出了面值多少元的PAT代金券 以及 排名为前K名的同学信息,允许并且


emmmmmmm

在读入的时候计算当前能获得多少代金券,并且累加

再按分数降序,邮箱升序的排序

最后输出的时候,如果当前这个人的成绩 与 前一个人的成绩 不相等, 那么就需要更新排名。且保证输出的这个排名要 比 最低排名的数 小

注: Java怎么样都是TLE的 150ms…


import java.io.*;
import java.util.*;

public class Main
{
	static class edge implements Comparable<edge>
	{
		String email; // 邮箱
		int score; // 成绩

		public edge(String email, int score)
		{
			this.email = email;
			this.score = score;
		}

		@Override
		public int compareTo(edge other)
		{
			if (score != other.score) return other.score - score; // 成绩降序
			return email.compareTo(other.email); // 邮箱升序
		}
	}

	static int get(int x, int g)
	{
		if (x >= g) return 50; // [G, 100] 区间内,可以得到50的
		if (x >= 60) return 20; // [60, G) 区间内,可以得到20的
		return 0; // 否则没有
	}

	public static void main(String[] args)
	{
		int n = sc.nextInt();
		int g = sc.nextInt();
		int k = sc.nextInt();

		edge shu[] = new edge[n + 10];
		shu[0] = new edge("", 0);

		int ans = 0; // 总面值
		for (int i = 1; i <= n; i++)
		{
			String email = sc.next();
			int score = sc.nextInt();
			shu[i] = new edge(email, score);
			ans += get(score, g); // 总面值 + 当前发出的
		}

		Arrays.sort(shu, 1, n + 1); // 排序

		out.println(ans);

		int cnt = 0; // 名次
		for (int i = 1; i <= n; i++)
		{
			if (shu[i].score != shu[i - 1].score) cnt = i; // 如果当前成绩 不等于 前一个人的成绩, 那么就需要更新排名
			if (cnt > k) break; // 如果 名次大于最低名次,则需要退出循环
			out.println(cnt + " " + shu[i].email + " " + shu[i].score);
		}

		out.flush();
		out.close();
	}

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);

}

c++

#include <iostream>
#include <algorithm>

using namespace std;    

const int N = 1e4 + 10;

struct edge
{
    string email;
    int score;
} shu[N];

int get(int x, int g)
{
    if(x >= g) return 50;
    if(x >= 60) return 20;
    return 0;
}

bool cmp(edge a, edge b)
{
    if(a.score != b.score) return a.score > b.score;
    return a.email < b.email;
}

int main()
{
    int n, g, k; cin >> n >> g >> k;
    int ans = 0;
    for(int i = 1; i <= n; i ++)
    {
        cin >> shu[i].email >> shu[i].score;
        ans += get(shu[i].score, g);
    }
    
    sort(shu + 1, shu + n + 1, cmp);
    
    cout << ans << endl;
    
    int res = 0, cnt = 0;
    for(int i = 1; i <= n; i ++)
    {
        res++;
        if(shu[i].score != shu[i - 1].score) cnt = res;
        if(res > k && cnt == res) break;
        cout << cnt << " " << shu[i].email << " " << shu[i].score << endl;
    }
    
    return 0;
}

对象数组排序
对象排序
对象排序


如果有说错的 或者 不懂的 尽管提 嘻嘻

一起进步!!!


闪现

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢谢 啊sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值