日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PAT1045 快速排序 (25 分)【4/6通过】

發(fā)布時(shí)間:2024/2/28 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT1045 快速排序 (25 分)【4/6通过】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目

代碼

第二次改進(jìn)
一個(gè)超時(shí),一個(gè)錯(cuò)誤

#include<iostream> #include<vector> #include<algorithm> using namespace std;struct Arr {long int value;bool flag = true; };int mySort(Arr a1, Arr a2) {return(a1.value < a2.value); }int main() {Arr a[100001];int total;cin >> total;//輸入int i;long int val;for (i = 0; i < total; i++){scanf("%ld",&val);a[i].value = val;}//遍歷改flagint j;int count = total;for (i = 0; i < total; i++){if (a[i].flag == true){for (j = i; j < total; j++){if (a[j].flag == true){if (a[i].value > a[j].value){if (a[i].flag == true){a[i].flag = false;count--;}if (a[j].flag == true){a[j].flag = false;count--;}}}}}}//直接在原來的數(shù)組排序sort(a, a + total - 1, mySort);cout << count << endl;for (i = 0; i < total; i++){if (a[i].flag == true){cout << a[i].value;count--;if (count != 0){cout << ' ';}}}cout << endl;//system("pause");return 0; }

暴力查找,超時(shí)

#include<iostream> #include<vector> #include<algorithm> using namespace std;int main() {long int a[100000];int total;cin >> total;//輸入int i;for (i = 0; i < total; i++){cin >> a[i];}//查找主元int before, after;vector<long int>center;for (i = 0; i < total; i++)//判斷第i個(gè)元素是否主元{//左邊小for (before = 0; before < i; before++){if (a[before] > a[i]){break;}}//右邊大for (after = i; after < total; after++){if (a[after] < a[i]){break;}}//如果能走完兩個(gè)循環(huán),說明是主元if (before == i && after == total){center.push_back(a[i]);}}//排序輸出sort(center.begin(), center.end());int size = center.size();cout << size << endl;for (i = 0; i < size; i++){cout << center[i];if (i != size - 1)cout << ' ';}cout << endl;system("pause");return 0; }

第一次改進(jìn)

#include<iostream> #include<vector> #include<algorithm> using namespace std;struct Arr {long int value;bool flag = true; };int mySort(Arr a1, Arr a2) {return(a1.value < a2.value); }int main() {Arr a[100000];int total;cin >> total;//輸入int i;long int val;for (i = 0; i < total; i++){cin >> val;a[i].value = val;}//遍歷改flagint j;for (i = 0; i < total; i++){if (a[i].flag == true){for (j = i; j < total; j++){if (a[j].flag == true){if (a[i].value > a[j].value){a[i].flag = false;a[j].flag = false;}}}}}//直接在原來的數(shù)組排序sort(a, a + total, mySort);//輸出int count = 0;for (i = 0; i < total; i++){if (a[i].flag == true){count++;}}cout << count << endl;for (i = 0; i < total; i++){if (a[i].flag == true){cout << a[i].value;count--;if (count != 0){cout << ' ';}}}cout << endl;//system("pause");return 0; }

總結(jié)

以上是生活随笔為你收集整理的PAT1045 快速排序 (25 分)【4/6通过】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。