求助,为什么nth_element过不去
查看原帖
求助,为什么nth_element过不去
759274
Stevehim楼主2022/10/27 10:45

RT

//算法库nth_element
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#define maxn 1010
using namespace std;

int n, k;
int a[maxn];
int temp;
int j;

int main() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> temp;
		int index = *find(a, a + j, temp) - 1;
		if (index >= 0) {
//			cout << "重复了" << endl;
			continue;
		} else {
			a[j] = temp;
			j++;
//			cout << a[j - 1] << endl;
		}
	}
//	for (int i = 0; i < j; i++) {
//		cout << a[i] << ' ';
//	}
//	cout << endl;
	nth_element(a, a + k, a + j);
	cout << a[k];
	return 0;
}

2022/10/27 10:45
加载中...