酷代码 AI
菜单
服务商

C++ 助力乌拉乎:快速从 n 个正整数中找出第 x 个数字

```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, x; cin >> n >> x; vector<int> nums(n); for (int i = 0; i < n; i++) { cin >> nums[i]; } sort(nums.begin(), nums.end()); cout << nums[x - 1] << endl; return 0; } ``` 程序说明: 1. 输入正整数个数n和查询位置x 2. 创建大小为n的vector存储所有数字 3. 对数组进行升序排序 4. 输出排序后第x个数字(索引为x-1) 时间复杂度:O(n log n),主要由排序操作决定 空间复杂度:O(n),用于存储输入数据 ######[AI问答 | 174点数解答 | 2025-11-14 20:18:48]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]