C++ Bc. 35 cpp

Z GeoWikiCZ
#include <iostream>
#include <iomanip>
#include <vector>
#include <ctime>

void hod_kostkami(std::vector<int>&);
bool alespon_na_jedne_kostce_padne_1_nebo_2(const std::vector<int>&);
bool padne_jedno_dve_nebo_tri_licha_cisla  (const std::vector<int>&);

int main()
{
  srand(time(0));             // inicializace generatoru rand()

  std::vector<int> kostky(4);

  double t1 = 0, s1 = 0;      // total, suma
  double t2 = 0, s2 = 0;

  const int M =    10;       // pocet opakovani pokusu
  const int N = 10000;       // pocet pokusu

  std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  std::cout.precision(4);

  for (int m=1; m<=M; m++)
    {
      for (int n=1; n<=N; n++)
        {
          hod_kostkami(kostky);
          if (alespon_na_jedne_kostce_padne_1_nebo_2(kostky)) s1++;
          if (padne_jedno_dve_nebo_tri_licha_cisla  (kostky)) s2++;
        }

      std::cout << std::setw(2) << m << "  |  ";
      t1 += s1;
      std::cout << s1/N   << "  " << t1/N/m << "  |  ";
      t2 += s2;
      std::cout << s2/N   << "  " << t2/N/m << "  |" << std::endl;
      s1 = s2 = 0;
    }

}

void hod_kostkami(std::vector<int>& kostky)
{
  for (unsigned int i=0; i<kostky.size(); i++)
    {
      kostky[i] = 1 + int( rand()/(RAND_MAX + 1.0)*6 );
    }
}

bool alespon_na_jedne_kostce_padne_1_nebo_2(const std::vector<int>& kostky)
{
  for (unsigned int i=0; i<kostky.size(); i++)
    {
      const int n = kostky[i];
      if (n == 1 || n == 2) return true;
    }

  return false;
}

bool padne_jedno_dve_nebo_tri_licha_cisla  (const std::vector<int>& kostky)
{
  int liche = 0;
  for (unsigned int i=0; i<kostky.size(); i++)
    {
      const int n = kostky[i];
      if (n%2 != 0) liche++;
    }

  return liche==1 || liche==2 || liche==3;
}

[ Zpět ]