返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     int t;
  8.     srand(time(NULL));
  9.     int a[10];
  10.     for(int k=0;k<=9;k++){
  11.         re:
  12.         a[k]=rand()%21;
  13.          for(int j=0; j<k; j++)
  14.         {
  15.             if(a[k]==a[j])
  16.             {
  17.                 goto re;
  18.             }
  19.         }
  20.     }



  21.     cout<<"排序前: ";
  22.     for(int i=0; i<=5; i++)
  23.     {
  24.         cout<<a[i]<<" ";
  25.     }
  26.     for(int i=0;i<=5;i++){
  27.         for(int j=i+1;j<=5;j++){
  28.             if(a[i]>a[j])
  29.             {
  30.                 t=a[j];
  31.                 a[j]=a[i];
  32.                 a[i]=t;
  33.             }

  34.         }


  35.     }

  36.     cout<<endl<<"排序後: ";
  37.     for(int i=0; i<=5; i++)
  38.     {
  39.         cout<<a[i]<<" ";
  40.     }

  41.     system("pause");

  42.     return 0;

  43. }
複製代碼

TOP

返回列表