A. The Rank

读错题意,WA了四次…
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 nn students, each of them has a unique id (from 11 to nn). Thomas’s id is 11. 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 nn (1≤n≤10001≤n≤1000) — the number of students.

Each of the next nn lines contains four integers aiai, bibi, cici, and didi (0≤ai,bi,ci,di≤1000≤ai,bi,ci,di≤100) — the grades of the ii-th student on English, German, Math, and History. The id of the ii-th student is equal to ii.

Output
Print the rank of Thomas Smith. Thomas’s id is 11.

Examples
inputCopy
5
100 98 100 100
100 100 100 100
100 100 99 99
90 99 90 100
100 98 60 99
outputCopy
2
inputCopy
6
100 80 90 99
60 60 60 60
90 60 100 60
60 100 60 80
100 100 0 100
0 0 0 0
outputCopy
1
Note
In the first sample, the students got total scores: 398398, 400400, 398398, 379379, and 357357. Among the 55 students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is 22.

In the second sample, the students got total scores: 369369, 240240, 310310, 300300, 300300, and 00. Among the 66 students, Thomas got the highest score, so his rank is 11.

#include <iostream>
#include <algorithm>
using namespace std;

struct node
{
    int a, b, c, d, sum;
} stu[1005];

int cmp(node a, node b)
{
    return a.sum > b.sum;
}

int main()
{
    int n;
    while(cin >> n)
    {
        if(n == 1)
        {
            cin >> stu[0].a >> stu[0].b >> stu[0].c >> stu[0].d;
            cout << '1' << '\n';
            continue;
        }
        bool flag = 1;
        int ans;
        cin >> stu[0].a >> stu[0].b >> stu[0].c >> stu[0].d;
        stu[0].sum = stu[0].a + stu[0].b + stu[0].c + stu[0].d;
        for(int i = 1; i < n; ++i)
        {
            cin >> stu[i].a >> stu[i].b >> stu[i].c >> stu[i].d;
            stu[i].sum = stu[i].a + stu[i].b + stu[i].c + stu[i].d;
        }
        sort(stu + 1, stu + n, cmp);
        for(int i = 1; i < n; ++i)
            if(stu[0].sum >= stu[i].sum)
            {
                flag = 0;
                ans = i;
                break;
            }
        if(!flag)
            cout << ans << '\n';
        else
            cout << n << '\n';

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This is a MySQL query that selects the student ID (s_id), assigns a sequential number to each row (i), and calculates the rank of each student based on their sum of scores (sum_score). The query uses a subquery to first group the scores by student ID and calculate the sum of scores for each student. This subquery is then joined with a variable initialization subquery that sets the initial values of @k, @i, and @score to 0. The variable @k is used to keep track of the current rank while iterating over the rows. The variable @i is used to assign a sequential number to each row. The variable @score is used to compare the sum_score of the current row with the sum_score of the previous row. The CASE statement is used to check if the sum_score of the current row is equal to the sum_score of the previous row. If they are equal, then the rank remains the same. If they are not equal, then the rank is updated to the current sequential number. Here is a breakdown of the query: 复制 SELECT a.s_id, -- Select the student ID @i:=@i+1 AS i, -- Assign a sequential number to each row @k:=(case when @score=a.sum_score then @k else @i end) as rank, -- Calculate the rank a.sum_score AS score -- Select the sum of scores for each student FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) a, -- Subquery to calculate sum of scores for each student (SELECT @k:=0,@i:=0,@score:=0) s -- Subquery to initialize variables Note that the use of variables in this query is not recommended, as it can lead to unexpected results if the variables are not reset properly. It is better to use a subquery or a window function to calculate the rank. 翻译
02-27

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值