C++ Bc. 38 cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
bool pokus(char[], int dim);
int main()
{
char listek[] = {'r', 'o', 'k', 'o', 'k', 'o'};
const int L = sizeof(listek);
std::srand(std::time(0)); // inicializace generatoru rand()
double soucet =0;
double celkem = 0;
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(4);
const int N = 1000000;
for (int k=1; k<=10; k++)
{
int s = 0;
for (int i=1; i<=N; i++)
if (pokus(listek, L))
s++;
std::cout << double(s)/N << std::endl;
soucet += s;
celkem += N;
}
std::cout << "-------\n";
std::cout << soucet/celkem << std::endl;
}
bool pokus(char text[], int dim)
{
// vypocet nahodne permutace
int k, i=dim;
while (i>0)
{
k = int( i*(rand()/(RAND_MAX + 1.0)) );
std::swap(text[--i], text[k]);
}
return text[0]=='k' && text[1]=='r' && text[2]=='o' && text[3]=='k';
}
[ Zpět ]