C++ Bc. 24 cpp: Porovnání verzí
Bez shrnutí editace |
m +kategorie programovani, c++ |
||
(Nejsou zobrazeny 2 mezilehlé verze od 2 dalších uživatelů.) | |||
Řádek 1: | Řádek 1: | ||
[ [[C | #include <iostream> | ||
#include <iomanip> | |||
#include <cstdlib> | |||
#include <ctime> | |||
using namespace std; | |||
bool simulace(int N) | |||
{ | |||
int rok[365] = {0}; | |||
for (int i=0; i<N; i++) | |||
rok[int(365.0*rand()/(RAND_MAX + 1.0))]++; | |||
for (int i=0; i<365; i++) | |||
if (rok[i] > 1) | |||
return true; | |||
return false; | |||
} | |||
float odhad_pravdepodobnosti(int pocet_pokusu, int pocet_osob) | |||
{ | |||
int test=0; | |||
for (int i=0; i<pocet_pokusu; i++) | |||
if (simulace(pocet_osob)) | |||
test++; | |||
return float(test)/float(pocet_pokusu); | |||
} | |||
int main() | |||
{ | |||
srand(time(0)); // inicializace generatoru rand() | |||
const int pocet_pokusu = 100000; | |||
cout << "\npocet pokusu v kazde simulaci je " << pocet_pokusu << "\n\n"; | |||
int pocet[] = {2, 3, 4, 5, 10, 15, 20, 23, 30, 40, 50}; | |||
cout.setf(ios_base::fixed, ios_base::floatfield); | |||
cout.precision(4); | |||
cout << "pocet osob pravdepodobnost\n"; | |||
for (int i=0; i<sizeof(pocet)/sizeof(int); i++) | |||
{ | |||
int pocet_osob = pocet[i]; | |||
cout << setw(9) << pocet_osob << " "; | |||
// odhad pravdepodobnosti pocitame vzdy t-krat | |||
for (int t=0; t<7; t++) | |||
cout << odhad_pravdepodobnosti(pocet_pokusu, pocet_osob) << " "; | |||
cout << endl; | |||
} | |||
} | |||
[ [[C++ Bc. 24 | Zpět ]] ] | |||
[[Kategorie:Programování]] | |||
[[Kategorie:C++]] |
Aktuální verze z 2. 9. 2006, 10:59
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; bool simulace(int N) { int rok[365] = {0}; for (int i=0; i<N; i++) rok[int(365.0*rand()/(RAND_MAX + 1.0))]++; for (int i=0; i<365; i++) if (rok[i] > 1) return true; return false; } float odhad_pravdepodobnosti(int pocet_pokusu, int pocet_osob) { int test=0; for (int i=0; i<pocet_pokusu; i++) if (simulace(pocet_osob)) test++; return float(test)/float(pocet_pokusu); } int main() { srand(time(0)); // inicializace generatoru rand() const int pocet_pokusu = 100000; cout << "\npocet pokusu v kazde simulaci je " << pocet_pokusu << "\n\n"; int pocet[] = {2, 3, 4, 5, 10, 15, 20, 23, 30, 40, 50}; cout.setf(ios_base::fixed, ios_base::floatfield); cout.precision(4); cout << "pocet osob pravdepodobnost\n"; for (int i=0; i<sizeof(pocet)/sizeof(int); i++) { int pocet_osob = pocet[i]; cout << setw(9) << pocet_osob << " "; // odhad pravdepodobnosti pocitame vzdy t-krat for (int t=0; t<7; t++) cout << odhad_pravdepodobnosti(pocet_pokusu, pocet_osob) << " "; cout << endl; } }
[ Zpět ]