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

  22.     for(int i=0;i<9;i++)
  23.     {
  24.        for(int j=i+1;j<10;j++)
  25.        {
  26.           if(n[j]<n[i])
  27.           {
  28.              tmp=n[j];
  29.              n[j]=n[i];
  30.              n[i]=tmp;         
  31.           }               
  32.        }
  33.     }
  34.     cout<<"10個範圍介於1~20之不重複隨機亂數, 由小而大依序為:"<<endl;
  35.     for(int i=0;i<10;i++)
  36.     cout<<n[i]<<" ";
  37.     cout<<endl;
  38.     system("pause");
  39.     return 0;         
  40. }   
複製代碼

TOP

返回列表