|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
這台機器有四顆功能鍵跟數字小鍵盤
功能鈕上分別寫著
1.Insert
2.Query MAX
3.Query MIN
4.Print Sorted Array
旁邊寫著一行粗字: 極值經查詢後將會刪除
題目看到這各位也明瞭了吧
請你寫出這台機器的程式可以插入數字並且查詢其中的最大值與最小值注意:
本題不可使用Max、Min、Sort()函式
輸入說明
每行輸入開頭有四種情形
1: 插入操作,其後會跟著一數字 N 代表插入的數字 (0 ≤ N ≤ 231-1)
2: 查詢最大值
3: 查詢最小值
4: 列印出排序好的陣列
同一時間內最多有 100,000 個數字
輸出說明
每筆查詢輸出一行
每行只有一個數字
範例輸入
1 3
1 100
2
1 4
3
範例輸出
100
3
求助 謝謝各位大佬了
你都已经用 C++ 了,为什么不用 vertor?
- #include <iostream>
- #include <vector>
- using std::vector;
- vector<int> Ver;
- void sort() {
- int N = Ver.size();
- for (int i = 0; i < N - 1; ++i) {
- for (int j = i + 1, t; j < N; ++j) {
- if (Ver[j] < Ver[i]) {
- t = Ver[i];
- Ver[i] = Ver[j];
- Ver[j] = t;
- }
- }
- }
- }
- using std::cout, std::endl;
- int main(void) {
- int arr[] = { 13, 7, 96, 58, 32 };
- for (const int &num: arr) {
- Ver.push_back(num);
- }
-
- sort();
-
- for (const int&num: Ver) {
- cout << num << " ";
- }
- cout << endl;
- cout
- << "最大值:" << Ver.back() << endl
- << "最小值:" << Ver[0] << endl;
- }
复制代码- 7 13 32 58 96
- 最大值:96
- 最小值:7
复制代码
|
|