21. Millionaire - TASK SOLUTION

Introduction

The task "Millionaire" is inspired by the popular quiz "Who Wants to Be a Millionaire?", in which contestants answer questions with four offered answers. Contestants have three lifelines: ask the audience, fifty-fifty and phone a friend. The goal of this task is to simulate the contestant's decision based on these lifelines.

Darko has reached the second-to-last question and has decided to use his strategy of combining these lifelines to reach the final answer. Your task is to follow his tactics and determine which answer Darko will give.

Instructions for solving

  1. Reading the data:
    • The first line of input contains four integers representing the number of audience votes for the answers a, b, c and d. All numbers are between 0 and 100 and their sum is 100.
    • If Darko did not give an answer based on the audience lifeline, the second line of input contains two different letters representing the answers the computer leaves after the fifty-fifty lifeline.
    • If a decision still cannot be made, the third line of input contains one letter representing the friend's suggestion.
  2. Ask the audience:
    • Compute the sum of the audience votes for all answers.
    • Check whether one of the answers has more votes than the sum of the others. If such an answer exists, print it immediately and end the program.
  3. Fifty-fifty:
    • The computer leaves two answers. Check the difference in votes between these two answers.
    • If the difference is at least 20 votes, choose the answer with more votes and end the program.
  4. Phone a friend:
    • If a decision still cannot be made after the second lifeline, read the friend's suggestion and print their choice.
  5. Output:
    • Print the final answer (one letter: a, b, c, or d).
#include <iostream> // Enables working with input and output
#include <cmath> // Enables using mathematical functions such as abs
using namespace std; // Uses the standard namespace
int main() {
            int a[4]; // Declares an array of 4 integer elements
            string o[4] = {"a", "b", "c", "d"}; // Labels for every element of the array
            bool largest = false; // Shows whether there is an element greater than the sum of the others
            for(int i = 0; i < 4; i++) {
                cin >> a[i]; // Reading the elements into the array
            }

            for(int i = 0; i < 4; i++) {
                // Computing the sum of all elements except the current one
                int sum = 0;
                for(int j = 0; j < 4; j++) {
                    if(j != i) {
                        sum += a[j]; // Adds the value if it is not the current element
                    }
                }
                if(a[i] > sum) {
                    cout << o[i] << endl; // Prints the label of the element greater than the sum of the others
                    largest = true;
                    break; // Stops further checking
                }
            }

            if(largest) {
                return 0; // Ends the program if the largest element is found
            }

            string half[2]; // Stores two elements for comparison
            cin >> half[0] >> half[1]; // Reading the labels for comparison
            int idxMax = -1, idxMax2 = -1;

            for(int i = 0; i < 4; i++) {
                if(o[i] == half[0]) {
                    idxMax = i; // Stores the index of the first element
                }
                if(o[i] == half[1]) {
                    idxMax2 = i; // Stores the index of the second element
                }
            }

            int diff = abs(a[idxMax2] - a[idxMax]); // Computes the absolute difference
            if(diff >= 20) {
                if(a[idxMax2] > a[idxMax]) {
                    cout << o[idxMax2] << endl; // Prints the label of the larger element
                } else {
                    cout << o[idxMax] << endl;
                }
            } else {
                string answer;
                cin >> answer; // Reading the additional answer
                cout << answer << endl; // Prints the user's input
            }

            return 0; // Ends the program
        }
    

Solution 2

#include <iostream> // Enables working with input and output
#include <vector> // Enables working with vectors
#include <string> // Enables working with strings
#include <algorithm> // Enables working with algorithms such as sorting
using namespace std; // Uses the standard namespace
int main() {
            // Number of votes for the answers a, b, c and d
            vector<int> votes(4);
            cin >> votes[0] >> votes[1] >> votes[2] >> votes[3];

            // Steps for ask the audience
            int sum = votes[0] + votes[1] + votes[2] + votes[3];
            for (int i = 0; i < 4; ++i) {
                if (votes[i] > sum - votes[i]) {
                    cout << (char)('a' + i) << endl;
                    return 0;
                }
            }

            // Steps for fifty-fifty
            char first, second;
            cin >> first >> second;

            int index1 = first - 'a';
            int index2 = second - 'a';

            if (abs(votes[index1] - votes[index2]) >= 20) {
                cout << (votes[index1] > votes[index2] ? first : second) << endl;
                return 0;
            }

            // Steps for phone a friend
            char friendsAnswer;
            cin >> friendsAnswer;

            cout << friendsAnswer << endl;
            return 0;
        }
    
Code explanation:
  • Reading the audience votes: We read four numbers representing the number of votes for the answers a, b, c and d.
  • Ask the audience:
    • We compute the sum of all votes.
    • We check whether one answer has more votes than all the others together (votes[i] > sum - votes[i]).
    • If the condition holds, we print that answer immediately and end the program.
  • Fifty-fifty:
    • We read the two answers that remain after using the fifty-fifty lifeline.
    • We compute the difference in the number of votes for these two answers.
    • If the difference is at least 20, we choose the answer with more votes.
  • Phone a friend:
    • If no answer meets the conditions of the previous steps, we read the friend's suggestion and choose it as the final answer.