Java 输入: 输入的第一行包含两个整数n和k(1≤k≤n≤50),一空格分开。 第二行包含n个用空格分开的整数Ai(0≤Ai≤100),表示排名第i的参赛者的得分。其中对任意的i满足Ai≥Ai+1。 输出: 输出将有多少人晋级下一轮。
时间: 2023-05-26 16:07:52 浏览: 46
Java代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] scores = new int[n];
for (int i = 0; i < n; i++) {
scores[i] = sc.nextInt();
}
int passScore = scores[k - 1];
int count = 0;
for (int score : scores) {
if (score >= passScore && score > 0) {
count++;
}
}
System.out.println(count);
}
}
相关问题
输入: 输入的第一行包含两个整数n和k(1≤k≤n≤50),一空格分开。 第二行包含n个用空格分开的整数Ai(0≤Ai≤100),表示排名第i的参赛者的得分。其中对任意的i满足Ai≥Ai+1。 输出: 输出将有多少人晋级下一轮。
算法思路:
首先将第K名参赛选手获取到的分数设为基准分,遍历每一个参赛选手,只要分数比基准分高且大于0,就可以晋级下一轮。
Python代码:
n, k = map(int, input().split())
scores = list(map(int, input().split()))
cut_off = scores[k-1] # 基准分数
count = 0 # 晋级人数
for score in scores:
if score >= cut_off and score > 0:
count += 1
print(count)
python输入有两行,第一行包含一个整数n(n小于100),代表样本容量;第二行包含n个绝
可以使用Python的input函数来实现输入两行,第一行是整数n,第二行是n个绝对值。
首先,使用input函数分别接收两行输入:
```python
n = int(input()) # 接收整数n
samples = input().split() # 接收n个绝对值,并使用split函数将其分割为一个列表
```
接下来,我们可以使用列表推导式将输入的字符串列表转换为整数列表:
```python
samples = [int(x) for x in samples]
```
最后,我们可以输出样本容量n和n个绝对值:
```python
print("样本容量为:", n)
print("绝对值列表为:", samples)
```
完整代码如下:
```python
n = int(input()) # 接收整数n
samples = input().split() # 接收n个绝对值,并使用split函数将其分割为一个列表
samples = [int(x) for x in samples] # 将字符串列表转换为整数列表
print("样本容量为:", n)
print("绝对值列表为:", samples)
```
注意:根据题目要求,样本容量n小于100,输入的绝对值个数也为n。