C++ Bc. 34 cpp

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

using namespace std;

// generator pseudonahodnych cisel <0, N)

int random(int N)
{
  return int( rand()/(RAND_MAX + 1.0)*N );  // int(vyraz) je explicitni konverze 
}


// nahodna permutace std vektoru - zamichani karet

void permutace(std::vector<int>& p)
{
  for (int i=p.size()-1; i>0; i--)   
    std::swap(p[i], p[random(i)]);
}


bool simulace(std::vector<int>& karty)
{
  // cervene zelene  kule  zaludy
  //    0      8      16     24    sedma 
  //    1      9      17     25    osma 
  //    2     10      18     26    devitka     
  //    3     11      19     27    desitka
  //    4     12      20     28    spodek
  //    5     13      21     29    filek
  //    6     14      22     30    kral
  //    7     15      23     31    eso

  while (true)
    {
      permutace(karty);
      
      int trumfy = 0;
      for (int i=0; i<=11; i++) if (karty[i] <= 7) trumfy++;

      if (trumfy != 4) continue;     // prvni hrac nema prave 4 trumfy

      int trumfy2 = 0;               // trumfy druheho hrace
      for (int i=12; i<=21; i++) if (karty[i] <= 7) trumfy2++;

      return trumfy2 == 0 || trumfy2 == 4;
    }
} 

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

  std::vector<int> karty(32);
  for (int i=0; i<32; i++) karty[i] = i;

  const int    N = 100000;           // pocet simulaci v jedne davce
  const double P = 0.086687;         // teoreticka pravdepodobnost 
  double tp = 0, tc = 0;             // soucty jevu ve vsech davkach
  
  cout.setf(ios_base::fixed, ios_base::floatfield);
  cout.precision(4);
  for (int n=1; n<=10; n++)
    {
      double p = 0;
      for (int i=0; i<N; i++) 
        if ( simulace(karty) )
          p++;                       // pocet priznivych jevu v davce

      tp += p;
      tc += N;

      cout << setw(2) << n << " : "
           << P << "  ~  "           // teoreticka hodnota pravdepodobnosti
           << p/N << "   "           // odhad z n-te davky
           << tp/tc << endl;         // odhad ze vsech davek
    }
}

[ Zpět ]