今天算是c++基础语言的最后一课,上了结构体这个神奇的东西,很好用。
做了一道题目,测了好多遍,总算AC,想要AC还是挺难的。
问题描述:
为了促进学生练习跳绳,某学校举行跳绳比赛,比赛规则为:每参赛者跳3次,按3次跳绳总数从高到低排名,如果3次跳绳总数相同,则按3次中跳的最多那次进行排序(从高到低),若还相同,则按出场顺序先后排序(先出场的排在前,后出场的排在后)。
【输入数据】
第一行一个整数n,表示共有n个人参加跳绳比赛;
以下n行,每行三个整数x y z,分别表示3次跳绳的个数,两数之间用一个空格隔开。
【输出数据】
输出n人排序后的出场序号(从1开始计数)和对应的跳绳总数。
每行两个整数,第1个整数表示出场序号,第2个整数表示对应的跳绳总数,两个整数之间用一个空格隔开。
【输入样例】
10
190 170 134
140 198 115
157 80 221
51 112 211
63 143 132
152 210 187
241 78 120
101 184 146
50 148 212
79 149 92
【输出样例】
6 549
1 494
3 458
2 453
7 439
8 431
9 410
4 374
5 338
代码如下:
结构体的确好写。
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
long first,second,third,p,max;
unsigned long sum;
}a[100001];
int comp(const student & a,const student & b)
{
if (a.sum>b.sum) return 1;
if (a.sum<b.sum) return 0;
if (a.max>b.max) return 1;
if (a.max<b.max) return 0;
if (a.p<b.p) return 1;
if (a.p>b.p) return 0;
}
int n;
int main()
{
freopen("skip.in","r",stdin);
freopen("skip.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%lld%lld%lld",&a[i].first,&a[i].second,&a[i].third);
a[i].sum=a[i].first+a[i].second+a[i].third;
a[i].p=i;
if((a[i].first>a[i].second)&&(a[i].first>a[i].third)) a[i].max=a[i].first;
if((a[i].second>a[i].first)&&(a[i].second>a[i].third)) a[i].max=a[i].second;
if((a[i].third>a[i].second)&&(a[i].third>a[i].first)) a[i].max=a[i].third;
}
sort(a+1,a+n+1,comp);
for(int i=1;i<=n;++i)
{
cout<<a[i].p<<' '<<a[i].sum<<endl;
}
fclose(stdin);fclose(stdout);
}