本帖最後由 蔡沛倢 於 2023-8-11 19:40 編輯
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int b;
- int a[]={5,8,9,4,10,2};
- //整理前
- cout<<"整理前:";
- for(int c=0;c<6;c++)
- {
- cout<<a[c]<<" ";
- }
- cout<<endl;
- //開始交換
- for(int i=0;i<5;i++)
- {
- for(int j=i+1;j<6;j++)
- {
- //判斷a[i]是否小於a[j]
- if(a[i]>a[j])
- {
- b=a[j];
- a[j]=a[i];
- a[i]=b;
- }
-
- }
- }
- //整理前
- cout<<"整理後:";
- for(int c=0;c<6;c++)
- {
- cout<<a[c]<<" ";
- }
- cout<<endl;
- system("pause");
- return 0;
- }
複製代碼 |