PAT L2-027 名人堂与代金券(25 分)

本文介绍了一种比赛成绩处理及排名输出的方法,通过C++实现,重点在于如何正确使用字符串比较函数strcmp进行选手姓名的排序,同时确保分数相同的选手排名一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

去年现场赛遇到的题目,当时能力还不够,没有做这道题

现在能做了,在最后并列输出的时候有点坑,并且千万注意字符串排序时候的strcmp函数返回的并不是0 1(好像是>0 <0这一类的),也不能直接楞比较两个字符大小,必须用strcmp

解决了这个问题以后谜一样的就从15变25了

题目链接
 

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct student
{
	char name[50];
	int score;
};
student s[10050];
int cmp(student a,student b)
{
	if(a.score==b.score)
	{
		if(strcmp(a.name,b.name)>0)
		{
			return 0;
		} //这里必须要使用strcmp 否则错 
		else
		{
			return 1;
		}
	}
	else
	{
		return a.score>b.score;
	}
}
int main(void)
{
	int n,g,k;
	scanf("%d%d%d",&n,&g,&k);
	int total = 0;
	for(int i=0;i<n;i++)
	{
		cin>>s[i].name>>s[i].score;
		if(s[i].score>=60&&s[i].score<g)
		{
			total += 20;
		}
		else if(s[i].score>=g&&s[i].score<=100)
		{
			total += 50;
		}
	}
	sort(s,s+n,cmp);
	printf("%d\n",total);
	int count = 0;
	int paiming[1005];
	for(int i=0;i<1005;i++)
	{
		paiming[i] = i+1;	
	}
	int temp;
	while(1)
	{
		printf("%d %s %d\n",paiming[count],s[count].name,s[count].score);
		temp = count;
		while(s[temp].score==s[temp+1].score)
		{
			paiming[temp+1] = paiming[temp];	
			temp++;
		}
		count++;
		if(count>=k&&s[count].score!=s[count-1].score)
		{
			break;
		}
	}
}
/*
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
*/
/*
10 80 7
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 78
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 87
jack@ucla.edu 88
bob@cmu.edu 79
ken@163.com 70
*/

 

### PTA Java L2-3 名人堂代金券解析 对于在中国大学MOOC上学习“数据结构”课程并通过PAT考试的学生,为了获得合格证书以及相应的PAT代金券奖励,学生需满足特定的成绩条件。具体而言: - 获得合格证书的要求是总评成绩至少为60- 成绩在[G, 100]区间内的考生可以获得50元PAT代金券- 成绩位于[60,G)区间的考生则可领取20元PAT代金券。 此外,教师会根据学生的最终得选出前K名加入名人堂,并公布这些顶尖学员的信息[^1]。 #### 输入描述 输入的第一行为三个整数N(参评选的人数)、G(划两类获奖者的数线)和K(进入名人堂的人数)。随后N行每行给出一位参赛者的数据,依次为其电子邮件地址(作为唯一识别符)及其对应的数。所有邮件地址均不含空白字符且长度不超过20个ASCII码单位;而数是一个介于0到100之间的整数值[^2]。 #### 输出说明 首行应显示发放给所有符合条件选手的PAT代金券总额。之后按照总评降序排列输出入选名人堂成员的具体情况——即其排名、邮箱地址及所获数。当存在多名同学拥有相同的最高时,则依据他们电子信箱字符串字典顺序从小至大进行排序展示。注意处理好并列情形下的正确表示方法[^3]。 ```java import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); // 参赛人数 int G = scanner.nextInt(); // 划标准线 int K = scanner.nextInt(); // 进入名人堂的数量 List<Student> students = new ArrayList<>(); long totalCouponValue = 0; for (int i = 0; i < N; ++i){ String email = scanner.next(); int score = scanner.nextInt(); Student student = new Student(email, score); if(score >= 60 && score < G){ totalCouponValue += 20; }else if(score >= G){ totalCouponValue += 50; } students.add(student); } System.out.println(totalCouponValue); Collections.sort(students, Comparator.comparing(Student::getScore).thenComparing(Student::getEmail).reversed()); Map<Integer, Integer> rankMap = getRankMap(students.subList(0,K)); for(int i=0;i<K;++i){ System.out.printf("%d %s %d\n",rankMap.get(i),students.get(i).getEmail(),students.get(i).getScore()); } } private static Map<Integer,Integer> getRankMap(List<Student> topStudents){ Map<Integer, Integer> map = new HashMap<>(); int currentRank = 1; for(int i=0 ; i<topStudents.size() ;++i){ if(i != 0 && !Objects.equals(topStudents.get(i).getScore(), topStudents.get(i - 1).getScore())){ currentRank = i + 1; } map.put(i,currentRank); } return map; } static class Student implements Comparable<Student>{ private final String email; private final int score; public Student(String email, int score){ this.email=email; this.score=score; } @Override public int compareTo(Student o){ return Integer.compare(o.score,this.score)==0?this.email.compareTo(o.email):Integer.compare(o.score,this.score); } public String getEmail(){ return email; } public int getScore(){ return score; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值