LINUX.ORG.RU

История изменений

Исправление legolegs, (текущая версия) :

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
enum COIN {
  GOLD,
  SILVER
};

struct chest {
  COIN coins[2];
};

const chest chests[] = {
  { {GOLD, GOLD} },
  { {GOLD, SILVER} },
  { {SILVER, SILVER} },
};
#define LEN(x) (sizeof(x)/sizeof((x)[0]))
int main()
{
  unsigned N = 10000000;
  unsigned chest_opened = 0;
  unsigned second_coin_gold = 0;
  srand ( time(NULL) );
  for (unsigned roll=0;roll<N;++roll)
  {
    const chest * ch = chests + rand()%LEN(chests);
    const int coin_index = rand()%2;
    if (ch->coins[coin_index] != GOLD)
      continue;
    ++chest_opened;
    const int other_coin_index = !coin_index;
    if (ch->coins[other_coin_index] == GOLD)
      ++second_coin_gold;
  }
  cout << "second coin is also gold in " << float(int(float(second_coing_gold)*1000/chest_opened))/10 << "% cases" << endl;
  return 0;
}

Ответ - уверенные 66.7%. Теперь разоблачение магии: в задаче не нужен СУНДУК с СЕРЕБРОМ, его можно закомментировать и результат не изменится. И вообще на самом деле после этого можно выкинуть сами сундуки, оставив монеты. ТРИ монеты, угадайте какие.

Исходная версия legolegs, :

Тред не полон без программы на c++

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
enum COIN {
  GOLD,
  SILVER
};

struct chest {
  COIN coins[2];
};

const chest chests[] = {
  { {GOLD, GOLD} },
  { {GOLD, SILVER} },
  { {SILVER, SILVER} },
};
#define LEN(x) (sizeof(x)/sizeof((x)[0]))
int main()
{
  unsigned N = 10000000;
  unsigned chest_opened = 0;
  unsigned second_coin_gold = 0;
  srand ( time(NULL) );
  for (unsigned roll=0;roll<N;++roll)
  {
    const chest * ch = chests + rand()%LEN(chests);
    const int coin_index = rand()%2;
    if (ch->coins[coin_index] != GOLD)
      continue;
    ++chest_opened;
    const int other_coin_index = !coin_index;
    if (ch->coins[other_coin_index] == GOLD)
      ++second_coin_gold;
  }
  cout << "second coin is also gold in " << float(int(float(second_coing_gold)*1000/chest_opened))/10 << "% cases" << endl;
  return 0;
}

Ответ - уверенные 66.7%. Теперь разоблачение багии: в задаче не нужен СУНДУК с СЕРЕБРОМ, его можно закомментировать и результат не изменится. И вообще на самом деле после этого можно выкинуть сундуки, оставив монеты. ТРИ монеты, угадайте какие.