返回列表 發帖

[作業] 產生不重複之隨機亂數 (三)

假設班上有五位同學: 子耕, 思惟, 璽安, 侑成, 挺桂.
試做一程式, 可隨機地抽選出三位同學. (抽籤的目的自由發揮)



本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int a, b, c, r;
  9.     string name[]={"子耕","思惟","璽安","侑成","挺桂"};
  10.     for(int i=1; i<=1; i++)
  11.     {
  12.         int a=-1, b=-1, c=-1;
  13.         cout<<"掃廁所三位同學: ";
  14.         for(int j=1; j<=3; j++)
  15.         {
  16.             r=rand()%4;
  17.             if(a!=r && b!=r && c!=r)
  18.             {
  19.                 cout<<name[r]<<" ";
  20.                 c=b;
  21.                 b=a;
  22.                 a=r;
  23.             }
  24.             else
  25.             {
  26.                 j--;
  27.             }
  28.         }
  29.         cout<<endl;
  30.         _sleep(500);
  31.     }
  32.     system("pause");
  33.     return 0;
  34. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int a, b, c, r;
  9.     string name[]={"子耕","思惟","璽安","侑成","挺桂"};
  10.     for(int i=1; i<=1; i++)
  11. {
  12.         int a=-1, b=-1, c=-1;
  13.         cout<<"負責打掃廁所的: ";
  14.         for(int j=1; j<=3; j++)
  15. {
  16.             r=rand()%4;
  17.             if(a!=r && b!=r && c!=r)
  18. {
  19.                 cout<<name[r]<<" ";
  20.                 c=b;
  21.                 b=a;
  22.                 a=r;


  23.             }
  24.             else{
  25.                 j--;
  26.             }
  27.         }
  28.         cout<<endl;
  29.         _sleep(500);
  30.     }
  31.     system("pause");
  32.     return 0;
  33. }
複製代碼

TOP

本帖最後由 陳妍蓁 於 2024-8-9 11:54 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     re:
  8.     system("cls");
  9.     srand(time(NULL));
  10.     string n[5]={" 子耕"," 思惟"," 璽安"," 侑成"," 挺桂"};
  11.     int x[3];

  12.     for(int i=0;i<3;i++){
  13.         x[i]=rand()%5;
  14.         for(int j=0;j<i;j++){
  15.             if(x[i]==x[j]){
  16.                 i--;
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.     cout<<"打掃的3位同學:";
  22.     for(int i=0;i<3;i++)
  23.         cout<<n[x[i]]<<endl;
  24.     cout<<endl;
  25.     system("pause");
  26.     goto re;
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int a, b, c, r;
  9.     string name[]={"子耕","思惟","璽安","侑成","挺桂"};
  10.     for(int i=1; i<=1; i++){
  11.         int a=-1, b=-1, c=-1;
  12.         cout<<"掃廁所的人有: ";
  13.         for(int j=1; j<=3; j++){
  14.             r=rand()%4;
  15.             if(a!=r && b!=r && c!=r){
  16.                 cout<<name[r]<<" ";
  17.                 c=b;
  18.                 b=a;
  19.                 a=r;
  20.             }
  21.             else{
  22.                 j--;
  23.             }
  24.         }
  25.         cout<<endl;
  26.         _sleep(500);
  27.     }
  28.     system("pause");
  29.     return 0;
  30. }
複製代碼

TOP

返回列表