返回列表 發帖
本帖最後由 蔡沛倢 於 2023-8-11 19:40 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {

  7.     int b;
  8.     int a[]={5,8,9,4,10,2};
  9.     //整理前
  10.     cout<<"整理前:";
  11.     for(int c=0;c<6;c++)
  12.     {
  13.         cout<<a[c]<<" ";
  14.     }
  15.         cout<<endl;
  16.         //開始交換
  17.     for(int i=0;i<5;i++)
  18.     {
  19.         for(int j=i+1;j<6;j++)
  20.         {  
  21.                     //判斷a[i]是否小於a[j]
  22.             if(a[i]>a[j])
  23.             {
  24.                b=a[j];
  25.                a[j]=a[i];
  26.                a[i]=b;
  27.             }
  28.                        
  29.         }
  30.     }
  31.         //整理前
  32.         cout<<"整理後:";
  33.         for(int c=0;c<6;c++)
  34.         {
  35.             cout<<a[c]<<" ";
  36.         }
  37.         cout<<endl;
  38.     system("pause");
  39.     return 0;
  40. }
複製代碼

TOP

返回列表