Sichuan University Programming Contest 2011 Preliminary(for Non-SCUers) / M A Simple Problem

本文介绍了一道关于ACM竞赛成绩排名的编程题,通过使用C++的sort函数配合自定义比较函数来解决该问题,并给出了两种不同的实现方式。

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

返回目录

 

题目大意: 背景是acm比赛的成绩排名规则,要求输出成绩最好的那个队。

 

题目类型: 排序题

 

题目分析: 利用 #include<algorithm>  的sort排序,自定义cmp函数。

 

代码:

#include<cstdio> #include<cstring> #include <algorithm> using namespace std; struct team { char name[20]; int num; int p; }; team a[102]; int cmp( const team &a, const team &b ) { if(a.num>b.num) return 1; if(a.num==b.num) { if(a.p < b.p) return 1; if(a.p == b.p) { if(strcmp(a.name, b.name) <0) return 1; } } return 0; } int main() { int t; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); for(int i=0; i<n; i++) scanf("%s%d%d", &a[i].name, &a[i].num, &a[i].p); sort(a, a+n, cmp); printf("%s/n", a[0].name); } }

 

官方标程:

利用了重载<运算符,再sort

#include<cstdio> #include<string> #include<iostream> #include<algorithm> #include<vector> #include<cstring> using namespace std; inline int Rint(){ int x; scanf("%d", &x); return x;} class tt //! { public: char str[100]; int s, p; bool operator < ( const tt & o )const { if( s != o.s )return s > o.s; if( p != o.p )return p < o.p; return strcmp(str, o.str) < 0; } }; int n; tt a[1000]; //重载了tt类型的<运算 int main() { int Tcase = Rint(); for(; Tcase -- ; printf("%s/n",a[0].str)) { n = Rint(); for( int i = 0; i < n; ++ i ) { scanf("%s", a[i].str); a[i].s = Rint(); a[i].p = Rint(); } sort(a, a + n); //! } return 0; }

转载于:https://www.cnblogs.com/tclh123/archive/2011/04/09/6171774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值