题目链接:
http://codeforces.com/problemset/problem/1017/A
Description
John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.
There are n students, each of them has a unique id (from 1 to n). Thomas’s id is 1. Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids.In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids.Please help John find out the rank of his son.
Input
The first line contains a single integer n(1≤n≤1000) — the number of students.
Each of the next n lines contains four integers ai, bi, ci, and di (0≤ai,bi,ci,di≤100) — the grades of the i-th student on English, German, Math, and History. The id of the i-th student is equal to i.
Output
Print the rank of Thomas Smith. Thomas’s id is 1
提示:
这是一道很简单的排序题,给出4个科分数,按从大到小排名,总分相同按id从小到大排。合理应用sort即可。
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 1001
using namespace std;
struct mrz
{
int id,sco;
} p[N];
bool cmp(mrz k1,mrz k2)
{
if(k1.sco!=k2.sco) return k1.sco>k2.sco;
else return k1.id<k2.id;
}
int n,ls;
int main()
{
scanf("%d",&n);
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++)
{
p[i].id=i;
for(int j=1;j<=4;j++)
{
scanf("%d",&ls);
p[i].sco+=ls;
}
}
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++) if(p[i].id==1) printf("%d\n",i);
return 0;
}
相关链接:
Codeforces 题解小全:
https://blog.csdn.net/ZJ_MRZ/article/details/81530091
Codeforces 1016A Death Note 题解:
https://blog.csdn.net/ZJ_MRZ/article/details/81474520
.