小团是某综艺节目的策划,他为某个游戏环节设计了一种晋级规则,已知在这个游戏环节中每个人最后都会得到一个分数score_i,显而易见的是,游戏很有可能出现同分的情况,小团计划该环节晋级人数为x人,则将所有人的分数从高到低排序,所有分数大于等于第x个人的分数且得分不为0的人都可以晋级。
请你求出本环节的实际晋级人数。显然这个数字可能是0,如果所有人的得分都是0,则没有人满足晋级条件。
小团是某综艺节目的策划,他为某个游戏环节设计了一种晋级规则,已知在这个游戏环节中每个人最后都会得到一个分数score_i,显而易见的是,游戏很有可能出现同分的情况,小团计划该环节晋级人数为x人,则将所有人的分数从高到低排序,所有分数大于等于第x个人的分数且得分不为0的人都可以晋级。
请你求出本环节的实际晋级人数。显然这个数字可能是0,如果所有人的得分都是0,则没有人满足晋级条件。
输入第一行包含两个正整数n和x,分别表示参加本环节的人数,和小团指定的x。
输入第二行包含n个整数,每个整数表示一位选手的得分。
输出仅包含一个整数,表示实际晋级人数。
5 4 0 0 2 3 4
3
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = 0, x = 0;
Integer[] nums;
while (scanner.hasNextInt()) {
n = scanner.nextInt();
x = scanner.nextInt();
nums = new Integer[n];
for (int i = 0; i < nums.length; i++)
nums[i] = scanner.nextInt();
//从大到校排列
Arrays.sort(nums, (a, b) -> b - a);
int i = x - 1;
if (nums[i].equals(0)) {
//削0
while (nums[i].equals(0))
i--;
} else {
//尝试查看右边数字是否和自身相同
while (i < nums.length && nums[i + 1].equals(nums[x - 1]))
i++;
}
System.out.println(i + 1);
}
}
} n, x = map(int, input().split()) score = [int(x) for x in input().split()] # print(score) score = sorted(score, reverse=True) # print(score) x_score = score[x-1] num = 0 for i in range(len(score)): if score[i] >= x_score and score[i] != 0: num += 1 print(num)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().trim().split(" ");
int n = Integer.parseInt(params[0]);
int x = Integer.parseInt(params[1]);
params = br.readLine().trim().split(" ");
int[] scores = new int[n];
for(int i = 0; i < n; i++)
scores[i] = Integer.parseInt(params[i]);
Arrays.sort(scores);
int count = x;
int baseline = scores[n - x];
if(scores[n - 1] == 0){
count = 0;
}else{
if(baseline > 0){
count --;
while(scores[n - x] == baseline){
count ++;
x ++;
}
}else{
while(scores[n - x] == baseline){
count --;
x --;
}
}
}
System.out.println(count);
}
} 计划该环节晋级人数为x人,则将所有人的分数从高到低排序,所有分数大于等于第x个人的分数且得分不为0的人都可以晋级,可能存在重复的分数情况;
例如
n=8 x=5
arr:0 1 1 1 2 3 4 5
排名第五的分数为arr[3]= 1;但是arr[0]和arr[1] 分数也为1;故也算晋级的人;
arr从小到大排序; base=arr[n-x]; base,则能晋级的人数为n-i import java.io.*;
import java.util.Arrays;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
int[] arr = new int[n];
s = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(s[i]);
}
int res = 0;
int len = arr.length;
Arrays.sort(arr);
int base = arr[n - x];
for(int i = 0; i < n; i++){
if(arr[i] == 0) continue;
else if(arr[i] == base) {
res =n - i;
break;
}
}
System.out.println(res);
}
}#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int n, x;
cin>>n>>x;
int res = x;
vector<int> nums(n, -1);
for(int i = 0; i < n; i++) cin>>nums[i];
sort(nums.rbegin(), nums.rend());
int index = x - 1;
if(nums[0] == 0) res = 0;
else{
if(nums[index] != 0) {
for(int i = index + 1; i < n; i++) {
if(nums[i] == nums[i - 1]) res++;
else break;
}
}else {
while(index >= 0 && nums[index] == 0){
index--;
res--;
}
}
}
cout<<res;
return 0;
} 读题真费劲,开始还写错了n, x = map(int, input().split()) score = [int(x) for x in input().split()] score = sorted(score) score.reverse() tem = 0 aim = score[x-1] new = [] for i in score: if i >= aim and i != 0: new.append(i) print(len(new))
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s1 = in.nextLine();
String s2 = in.nextLine();
int n = Integer.parseInt(s1.split(" ")[0]);
int x = Integer.parseInt(s1.split(" ")[1]);
int[] scores = new int[n];
String[] str = s2.split(" ");
for (int i = 0; i < n; i++) {
scores[i] = Integer.parseInt(str[i]);
}
Arrays.sort(scores);
int res = x;
int line = scores[n - x];
for (int i = n - x - 1; i >= 0; i--) {
if (scores[i] >= line) {
res++;
}else {
break;
}
}
System.out.println(res);
}
} package main
import (
"fmt"
"sort"
)
func Win(peo []int, x int) int {
sort.Ints(peo)
var res int
for i:=len(peo)-1;i>=len(peo)-x;i--{
if peo[i]==0{
return res
}
if i==len(peo)-x&&peo[i]==peo[i-1]{
res++
x++
continue
}
res++
}
return res
}
func main() {
var n,x int
fmt.Scan(&n,&x)
peo:=make([]int,n)
for i:=0;i<n;i++{
fmt.Scan(&peo[i])
}
res:=Win(peo,x)
fmt.Println(res)
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Test02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
int[]ans = new int[n];
String[] str1 = br.readLine().split(" ");
for(int i=0;i<n;i++){
ans[i] = Integer.parseInt(str1[i]);
}
Arrays.sort(ans);
int i=0;
for(i=n-x-1;i<n;i++){
if(ans[i]!=0)break;
}
if(i<n){
for(;ans[i]!=0&&i-1>=0;i--) {
if (ans[i] != ans[i - 1]) break;
}
}
System.out.println(n-i);
}
}
简简单单排个序
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] arr = br.readLine().split(" ");
int n = Integer.valueOf(arr[0]), x = Integer.valueOf(arr[1]);
Integer[] scores = Arrays.stream(br.readLine().split(" ")).map(Integer::valueOf).filter(v -> v > 0).toArray(Integer[]::new);
Arrays.sort(scores, (o1, o2) -> o2 - o1);
if (scores.length <= x) {
bw.write(String.valueOf(scores.length));
} else {
int i = x;
while (i < scores.length && scores[i].equals(scores[x - 1])) i++;
bw.write(String.valueOf(i));
}
bw.flush();
}
}
var line1=readline().split(' ')
var n=parseInt(line1[0])
var x=parseInt(line1[1])
var line2=readline().split(' ')
var score=[]
for(let i=0;i<n;i++){
score.push(parseInt(line2[i]))
}
score=score.sort((a,b)=>b-a)
if(score[0]==0){
print(0)
}else{
var num=0
let sx=score[x-1]
for(let i=0;i<n;i++){
if(score[i]>=sx && score[i]>0){
num++
}
}
print(num)
}
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int x = in.nextInt();
Integer[] a = new Integer[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a, (o1, o2) -> o2-o1);
int ans = 0;
for(int i = 0; i < n; i++) {
if(a[i] != 0 && a[i] >= a[x-1]) {
ans++;
}
}
System.out.println(ans);
}
}