online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <vector> #include <string> struct Box { std::string Ball1; std::string Ball2; }; int main() { // Коробки с шарами std::vector<Box> boxes; boxes.push_back({ "gold", "gold" }); boxes.push_back({ "gold", "silver" }); //boxes.push_back({ "silver", "silver" }); // Тянем шары const int N = 1000000; int winCount = 0; int chosen_index; std::string chosen_ball; std::string remaining_ball; for (int i = 0; i < N; i++) { rechoseBox: // Выбор случайной коробки Box box = boxes[std::rand() % boxes.size()]; if (box.Ball1 != "gold" && box.Ball2 != "gold") { // Не возможный случай по условию (коллапс вселенной) => выбираем коробку ещё раз goto rechoseBox; } rechoseBall: // 1) Тянем первый шар из коробки, он должен оказаться золотой (другие случаи по условию не возможны) chosen_index = std::rand() % 2; if (chosen_index == 0) { chosen_ball = box.Ball1; remaining_ball = box.Ball2; } else { chosen_ball = box.Ball2; remaining_ball = box.Ball1; } if (chosen_ball == "silver") { // Не возможный случай по условию (коллапс вселенной) => выбираем коробку ещё раз goto rechoseBall; } // 2) Рандомно тянем второй шар из той же коробки и увеличиваем счётчик, если он оказался золотым winCount += remaining_ball == "gold"; } std::cout << "Answer = " << (double)winCount / N << "\n"; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue