// clang++ -std=c++23 -Wall -Wextra -pedantic -D_CRT_SECURE_NO_WARNINGS elimination_analysis.cpp -g -o elimination_analysis.exe // .\elimination_analysis.exe 7 #include #include std::vector analyzeRps(int n) { std::vector remainOccur(n + 1); // All n players stay (0 get eliminated) // in 3 ^ n minus the number of other combinations. remainOccur[n] = 1; for (int i = 0; i < n; ++i) remainOccur[n] *= 3; // i players stay if they choose a winning option (eg. rock) // while all other n - i players choose the losing option (eg. scissors). long nextNChooseK = n; for (int i = 1; i < n; ++i) { remainOccur[i] = 3 * nextNChooseK; remainOccur[n] -= remainOccur[i]; nextNChooseK = nextNChooseK * (n - i) / (i + 1); } return remainOccur; } std::vector analyzeZzz(int n) { std::vector remainOccur(n + 1); // i players stay if their option is in the majority. // This means that either all or fewer than n / 2 players can stay. long nextNChooseK = n; for (int i = 1; i < (n + 1) / 2; ++i) { remainOccur[i] = 2 * nextNChooseK; nextNChooseK = nextNChooseK * (n - i) / (i + 1); } // All n players stay (0 get eliminated) // if all choose the same option // or if it's a tie between the two options. remainOccur[n] = 2; // If n is even, nextNChooseK now equals n choose n / 2. if (n % 2 == 0) remainOccur[n] += nextNChooseK; return remainOccur; } void printAnalysis(const std::vector &remainOccur) { for (size_t i = 0; i < remainOccur.size(); ++i) { std::cout << i << ": " << remainOccur[i] << std::endl; } long total = 0, weighted = 0; for (size_t i = 0; i < remainOccur.size(); ++i) { total += remainOccur[i]; weighted += i * remainOccur[i]; } std::cout << "Avg: " << weighted << "/" << total << "=" << (double)weighted / total << std::endl; } int main(int argc, char *argv[]) { if (argc != 2) return 1; int n; if (std::sscanf(argv[1], "%d", &n) != 1) return 1; if (n < 3 || n > 15) return 1; std::cout << "ROCK PAPER SCISSORS" << std::endl; std::cout << "===================" << std::endl; printAnalysis(analyzeRps(n)); std::cout << std::endl; std::cout << "ZIMI ZAMI ZUM" << std::endl; std::cout << "=============" << std::endl; printAnalysis(analyzeZzz(n)); return 0; }