ACM NextRound ——最终没有用到排序

文章讲述了如何使用C++的sort()函数对参赛者的分数进行排序,并基于排序结果计算出能晋级到下一轮的参赛者人数。程序首先读取参赛人数n和晋级名额k,然后对分数数组进行排序,检查第k个位置的分数是否大于0,最后统计等于该分数的参赛者数量,输出晋级人数。
摘要由CSDN通过智能技术生成

排序sort()函数

参考 : (8条消息) C++ sort()排序详解_晴空๓的博客-CSDN博客

"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.

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.

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

//下面是自作聪明
/*
bool cmp(int a, int b)
{
	return a > b;
}
int main()
{
	int n, k;
	cin >> n >> k;
	int* people = new int[n];
	for (int i = 0; i < n; i++) cin >> people[i];
	sort(people, people + n, cmp);
	//for (int i = 0; i < n; i++) cout << people[i] << " ";
	if (people[k - 1] <=0) {
		cout << "0";
		return 0;
	}
	int cnt_more = 0;
	while (people[k + cnt_more] == people[k - 1]) cnt_more++;
	cout << (k + cnt_more);
	//排序到递减
	//第k名如果等于0,输出0
	//while(k+cnt的等于k) cnt++
	
	//按照道理,我应该手搓一个排序算法
	//但我不想搓
	return 0;
}
*/
//tmd这跟排序没有一点关系
int main()
{
	int n, k;
	cin >> n >> k;
	int* people = new int[n];
	for (int i = 0; i < n; i++) {
		cin >> people[i];
	}
	int K_people = people[k - 1];
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		if (people[i] >= K_people&&people[i]!=0) cnt++;
	}
	cout << cnt;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值