HDU 2148 Score

http://acm.hdu.edu.cn/showproblem.php?pid=2148

Problem Description
转眼又到了一年的年末,Lele又一次迎来了期末考试。虽然说每年都要考试,不过今年的这场考试对Lele来说却意义重大。

因为经济原因,如果今年没有排在班级前几名,而拿不到奖学金的话,家里便无力再供他继续读书。而且家里帮他都想好出路了——回家种田!!

虽说Lele心里有一百个不愿意,不过父母的话不能不听。

忐忑不安地考完试,Lele拿到了全班的成绩单,这张成绩单是按学号顺序排好的。Lele很想知道班里到底有多少人分数比他高,现在就请你帮帮他,帮他数一下到底有多少人的分数比他高吧。
 
Input
数据的第一行有一个正整数T,表示测试的组数。接下来有T组测试。
每组数据包括两行。
第一行有两个正整数N K(0<N<1000,0<K<=N),分别表示成绩单上一共的学生数目,和Lele的学号。
第二行有N个整数Xi(0<=Xi<=100)分别表示各个学生的成绩,以学号递增顺序给出,第一个学生学号为1。
 
Output
对于每组数据,请在一行里输出班里一共有多少个学生成绩高于Lele
 
Sample Input
1
3 2
81 72 63
 
Sample Output
1
 
时间复杂度:$O(N)$
代码:
#include <bits/stdc++.h>
using namespace std;

int N, K, T;
int x[1010];

struct Students {
    int num;
    int score;
}students[1010];

int main() {
    scanf("%d", &T);
    while(T --) {
        int cnt = 0;
        scanf("%d%d", &N, &K);
        for(int i = 1; i <= N; i ++) {
            scanf("%d", &students[i].score);
            students[i].num = i;
        }
        
        for(int i = 1; i <= N; i ++) {
            if(students[i].score > students[K].score)
                cnt ++;
        }
        printf("%d
", cnt);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zlrrrr/p/9636950.html