返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    srand(time(NULL));
  7.    int n[4];
  8.    cout<<"4 個介於0~9 的不重複隨機亂數為:"<<endl;
  9.    for (int i=0; i<4; i++)
  10.    {
  11.      n[i]=rand()%10;
  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<4; i++)
  22.    {
  23.    cout<<n[i]<<" ";
  24.    }
  25.    cout<<endl;
  26.    system("pause");
  27.    return 0;   
  28. }
複製代碼

TOP

返回列表