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

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

现在能做了,在最后并列输出的时候有点坑,并且千万注意字符串排序时候的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
*/

 

### L2-027 Python 问 练习 解决方案 对于L2-027的具体目描述未直接提供,但从相似类型的编程竞赛目来看,通常涉及算法设计、数据结构应用以及复杂度析等内容。基于此背景,可以推测该可能属于中级难度的算法挑战。 #### 可能的主方向 考虑到L2级别的目往往聚焦于更复杂的逻辑处理和优化技巧,这类问可能会涉及到但不限于以下几个方面: - 动态规划的应用 - 高效的数据检索机制实现 - 复杂图形操作或路径寻找算法 为了更好地理解如何解决此类问,下面给出一个假设性的例子,展示了一个关于字符串匹配的问解决方案,这可能是L2级别中常见的主之一。 ```python def find_substring(text, pattern): """ 实现KMP算法用于高效子串查找 参数: text (str): 主串 pattern (str): 子串 返回: int: 如果找到则返回起始位置;否则返回-1表示不存在这样的子串 """ lps = compute_lps_array(pattern) i = 0 # index for text[] j = 0 # index for pattern[] while i < len(text): if pattern[j] == text[i]: i += 1 j += 1 if j == len(pattern): return i-j j = lps[j-1] elif i < len(text) and pattern[j] != text[i]: if j != 0: j = lps[j-1] else: i += 1 return -1 def compute_lps_array(pat): length = 0 # length of the previous longest prefix suffix lps = [0]*len(pat) i = 1 while i < len(pat): if pat[i]==pat[length]: length+=1 lps[i]=length i+=1 else : if length!=0: length=lps[length-1] else: lps[i]=0 i+=1 return lps ``` 上述代码实现了经典的Knuth-Morris-Pratt(KMP)模式匹配算法[^1],适用于快速定位较长文本内的特定短语或单词序列的位置。这种技术不仅提高了搜索效率,在实际应用场景中有广泛用途,而且也是许多高级编程比赛中考察的重点技能之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值