返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main(){
  6.     srand(time(NULL));
  7.     int n[10],tmp;
  8.     re:
  9.     for(int i=0; i<10; i++)
  10.     {
  11.          n[i]=rand()%20+1;
  12.          for(int j=0; j<i; j++)
  13.          {
  14.              if(n[i]==n[j])
  15.              {
  16.                  i--;
  17.                  break;
  18.              }      
  19.          }
  20.     }
  21.     for(int i=0; i<9; i++)
  22.     {
  23.          for(int j=i+1; j<10; j++)
  24.          {
  25.              if(n[j]<n[i])
  26.              {
  27.                  tmp=n[j];
  28.                  n[j]=n[i];
  29.                  n[i]=tmp;
  30.              }   
  31.          }
  32.     }
  33.     cout<<"10個範圍介於1~20之不重複隨機亂數, 由小而大依序為:"<<endl;
  34.     for(int i=0; i<10; i++)
  35.         cout<<n[i]<<" ";
  36.     cout<<endl;
  37.     system("pause");
  38.     goto re;
  39.     return 0;
  40. }
複製代碼

TOP

返回列表