C++ 实现数组降序排序后前 30% 最小值
以下是用 C++ 实现的代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n = 10; // 数组长度
int arr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; // 原始数组
sort(arr, arr + n, greater<int>()); // 降序排序
int k = n * 0.3; // 前 30% 的元素个数
int min = arr[k - 1]; // 前 30% 中最小的数
cout << '前 30% 中最小的数是:' << min << endl;
return 0;
}
这个程序首先定义了一个长度为 10 的数组,然后使用 sort() 函数对数组进行降序排序。接着计算前 30% 的元素个数,并取出前 30% 中最小的数。最后输出结果。
原文地址: https://www.cveoy.top/t/topic/ouMe 著作权归作者所有。请勿转载和采集!