Codeforces 1017A The Rank 题解

博客详细解析了Codeforces 1017A题目的解决方案,该题要求根据学生考试成绩进行排名。输入包含学生人数和各科分数,输出学生Thomas Smith(ID为1)的排名。题目强调根据总分降序排列,总分相同时按ID升序。解题关键在于正确使用排序算法。
摘要由CSDN通过智能技术生成

题目链接:

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
.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值