158A. Next Round
- time limit per test3 seconds
- memory limit per test256 megabytes
- inputstandard input
- outputstandard output
"Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..." — an excerpt from contest rules.
“得分等于或大于第k名选手得分的选手将进入下一轮,只要该选手得分为正……”选自竞赛规则。
A total of n participants took part in the contest (n ≥ k), and you already know their scores. Calculate how many participants will advance to the next round.
共有n名参与者参加了比赛(n ≥ k) ,你已经知道他们的分数了。计算有多少参与者将进入下一轮。
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.
输入的第一行包含两个整数n和k(1) ≤ K ≤ N ≤ 50)被一个单独的空间隔开。
The second line contains n space-separated integers a, a, ..., a (0 ≤ a ≤ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: a ≥ a).
第二行包含n个空格分隔的整数a, a, ...,a(0 ≤ a ≤ 100),其中a是获得第i名的参与者获得的分数。给定的序列是非递增的(即,对于从1到n-1的所有i)满足以下条件:a ≥ a).
Output
Output the number of participants who advance to the next round.
输出进入下一轮的参与者人数。
Examples
input1
8 5
10 9 8 7 7 7 5 5
output1
6
input2
4 2
0 0 0 0
output2
0
Note
In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.
在第一个例子中,排名第五的参与者获得7分。由于第六名的参与者也获得了7分,因此有6名获得者。
In the second example nobody got a positive score.
在第二个例子中,没有人得到正面分数。
Solution
考虑两个条件:
- 得分等于或大于第k名选手得分(a[i] >= a[k-1])
- 选手得分为正(a[i] > 0)
Code
#include <iostream>
using namespace std;
//158A. Next Round
int main() {
int n = 0;//n:n名参与者
int k =0;//k:得分等于或大于第k名选手得分
cin >> n >> k;
int sum = 0;//sum:进入下一轮的参与者人数
int a[n];//a[i]:获得第i名的参与者获得的分数
for(int i = 0;i < n;i++){
cin >> a[i];
}
int flag = a[k-1];
for(int i = 0;i < n;i++) {
if (a[i] >= flag && a[i] > 0) {
sum++;
}else{
break;
}
}
cout << sum << endl;
return 0;
}