Advertisement
totobac

Untitled

Nov 27th, 2021
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6.  
  7. bool isWantedCombination(int dices[5], int& repeatedNumber) {
  8.     for (size_t i = 0; i < 5; i++)
  9.     {
  10.         int currentCounter = 1;
  11.         for (size_t j = i + 1; j < 5; j++)
  12.         {
  13.             if (dices[i] == dices[j])
  14.             {
  15.                 currentCounter++;
  16.             }
  17.         }
  18.         if (currentCounter >= 3)
  19.         {
  20.             repeatedNumber = dices[i];
  21.             return true;
  22.         }
  23.     }
  24.     return false;
  25. }
  26.  
  27. bool isReady(bool flags[6]) {
  28.     for (size_t i = 0; i < 6; i++)
  29.     {
  30.         if (flags[i] == false)
  31.         {
  32.             return false;
  33.         }
  34.     }
  35.     return true;
  36. }
  37.  
  38.  
  39.  
  40. int getSuccesfulTry() {
  41.     srand(time(NULL));
  42.     int counter = 0;
  43.     bool flags[6] = { false };
  44.  
  45.     while (!isReady(flags))
  46.     {
  47.        
  48.         int dices[5];
  49.         for (size_t i = 0; i < 5; i++)
  50.         {
  51.             dices[i] = (rand() % 6 + 1);
  52.         }
  53.         int repeatedNumber = 0;
  54.         if (isWantedCombination(dices, repeatedNumber))
  55.         {
  56.             flags[repeatedNumber - 1] = true;
  57.         }
  58.         counter++;
  59.     }
  60.    
  61.     return counter;
  62. }
  63.  
  64.  
  65.  
  66. int main()
  67. {
  68.        
  69.     std::cout << getSuccesfulTry();
  70.    
  71.  return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement