返回列表 發帖
  1. #include<cstdlib>
  2. #include<iostream>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int n[10], x;
  9.     for(int j=0; j<10; j++)
  10.     {
  11.         n[j]=rand()%20+1;
  12.         for(int k=0; k<j; k++)
  13.         {
  14.             if(n[j]==n[k])
  15.             {
  16.                j--;
  17.                break;
  18.             }
  19.         }
  20.     }
  21.     cout<<"排序前"<<endl;
  22.     for(int j=0; j<10; j++)
  23.     {
  24.         cout<<n[j]<<" ";
  25.     }
  26.     cout<<endl<<endl;
  27.     for(int i=0; i<10; i++)
  28.     {
  29.         for(int j=0; j<10; j++)
  30.         {
  31.             if(n[i]>n[j])
  32.             {
  33.                 x=n[j];
  34.                 n[j]=n[i];
  35.                 n[i]=x;
  36.             }
  37.         }

  38.     }
  39.     cout<<endl<<"排序後: "<<endl;
  40.     for(int i=0; i<10; i++)
  41.     {
  42.         cout<<n[i]<<" ";
  43.     }


  44. }
複製代碼

TOP

返回列表