返回列表 發帖
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     int x;
  8.     srand(time(NULL));
  9.     cout<<"10個範圍介於1~20之不重複隨機亂數, 由小而大依序為: "<<endl;
  10.     int a[10];
  11.     for(int i=0; i<=9; i++)
  12.     {
  13.         re:
  14.         a[i]=rand()%38+1;
  15.         for(int j=0; j<i; j++)
  16.         {
  17.             if(a[i]==a[j])
  18.                 goto re;
  19.         }
  20.     }
  21.     for(int i=0; i<=9; i++)
  22.     {
  23.         cout<<a[i]<<" ";
  24.     }

  25.     for(int i=0; i<=9; i++)
  26.     {
  27.         for(int j=0; j<=9; j++)
  28.         {
  29.             if(a[i]<a[j])
  30.             {
  31.                 x=a[j];
  32.                 a[j]=a[i];
  33.                 a[i]=x;
  34.             }
  35.         }

  36.     }
  37.     cout<<endl;
  38.     for(int i=0; i<=9; i++)
  39.     {
  40.         cout<<a[i]<<" ";
  41.     }
  42.     cout<<endl;
  43.     system("pause");
  44.     return 0;
  45. }
複製代碼

TOP

返回列表