桶排40求助
查看原帖
桶排40求助
229218
最萌軒宇楼主2022/10/6 21:39

先上代码:

#include <iostream>
using namespace std;
int n, k, a[10010] = {};
int main(){
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		int x;
		cin >> x;
		a[x]++;
	}
	int m = 0;
	for(int i = 0; i < n; i++){
		if(a[i] != 0){
			m++;
		}
		if(m == k){
			cout << i;
			return 0;
		}
	}
	cout << "NO RESULT";
	return 0;
}

想问一下这题桶排可不可行?

我的思路是,

把相同的装进一个桶,最后以m==k,

桶名k相同时输出i得出第k小的数,

请dalao们指出错误,蟹蟹:)

2022/10/6 21:39
加载中...