Untitled
unknown
plain_text
2 years ago
2.0 kB
4
Indexable
#include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; // Function to check consecutive occurrences bool checkConsecutive(const string &str, char ch, int consecutiveCount) { int count = 0; for (char c : str) { if (c == ch) { count++; if (count >= consecutiveCount) { return true; } } else { count = 0; } } return false; } // Function to output colored text void colorOutput(const string &str) { for (char c : str) { if (c == 'H') { cout << "\033[1;31m" << c << "\033[0m"; // Red color for 'H' } else { cout << "\033[1;34m" << c << "\033[0m"; // Blue color for 'T' } } cout << endl; } int main() { int t; cin >> t; while (t--) { // Seed the random number generator srand(static_cast<unsigned int>(time(nullptr))); string coinFlipResult; // String to store the results // Simulate 1000 coin flips for (int i = 0; i < 1000; ++i) { int result = rand() % 2; // Generate random number (0 or 1) // If the result is 0, add 'H' (heads), else add 'T' (tails) to the string if (result == 0) { coinFlipResult += 'H'; } else { coinFlipResult += 'T'; } } // Check consecutive occurrences and print accordingly if (checkConsecutive(coinFlipResult, 'H', 15)) { cout << "H appeared 15 or more times consecutively.\n"; } if (checkConsecutive(coinFlipResult, 'T', 15)) { cout << "T appeared 15 or more times consecutively.\n"; } cout << "Both didn't appear.\n"; colorOutput(coinFlipResult); // Output colored result } }
Editor is loading...