C++ Bc. 40 cpp: Porovnání verzí
BUG !!! |
varianta JB |
||
Řádek 133: | Řádek 133: | ||
std::cout << b << "/" << c << ": " | std::cout << b << "/" << c << ": " | ||
<< simulace(nexp, b, c) << " "; | << simulace(nexp, b, c) << " "; | ||
} | |||
std::cout << "\n"; | |||
} | |||
} | |||
</pre> | |||
Varianta s testem vzájemně se ohrožujících dam podle Jiřího Bartoše. | |||
<pre> | |||
#include <iostream> | |||
#include <cmath> | |||
#include <ctime> | |||
double r() { return rand()/(RAND_MAX + 1.0); } | |||
bool nahodne_postaveni(int bile, int cerne) | |||
{ | |||
enum {bila=1, cerna=2}; | |||
int sachovnice[8][8] = {{0}}; | |||
int pocet = 0; | |||
int figury[64]; | |||
int figrad[64]; | |||
int figslp[64]; | |||
int pocet_barev[3]; | |||
pocet_barev[bila ] = bile; | |||
pocet_barev[cerna] = cerne; | |||
for (int barva=bila; barva<=cerna; barva++) | |||
for (int b=1; b<=pocet_barev[barva]; b++) | |||
{ | |||
int i; | |||
int j; | |||
do | |||
{ | |||
i = int( 8*r() ); | |||
j = int( 8*r() ); | |||
} | |||
while (sachovnice[i][j] != 0); | |||
figury[pocet] = sachovnice[i][j] = barva; | |||
figrad[pocet] = i; | |||
figslp[pocet] = j; | |||
pocet++; | |||
} | |||
for (int i=0; i<pocet; i++) | |||
for (int j=i+1; j<pocet; j++) | |||
if (figury[i] != figury[j]) | |||
{ | |||
using std::abs; | |||
const int ri = figrad[i]; | |||
const int rj = figrad[j]; | |||
const int si = figslp[i]; | |||
const int sj = figslp[j]; | |||
if (ri == rj || si == sj) return false; // radky a sloupce | |||
if (abs(ri - rj) == abs(si - sj)) return false; // obe diagonaly | |||
} | |||
return true; | |||
} | |||
double simulace(int pokusy, int bile, int cerne) | |||
{ | |||
int priznive_jevy = 0; | |||
for (int i=0; i<pokusy; i++) | |||
if (nahodne_postaveni(bile, cerne)) | |||
priznive_jevy++; | |||
return double(priznive_jevy)/pokusy; | |||
} | |||
int main() | |||
{ | |||
std::cout.precision(2); | |||
std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); | |||
const int nexp = 1000000; | |||
std::srand(std::time(0)); | |||
for (int b=1; b<=4; b++) | |||
{ | |||
for (int c=1; c<=b; c++) | |||
{ | |||
std::cout << b << "/" << c << ": " | |||
<< simulace(nexp, b, c) << " "; | |||
} | } | ||
std::cout << "\n"; | std::cout << "\n"; |
Verze z 25. 1. 2008, 11:36
#include <iostream> #include <cmath> #include <ctime> double r() { return rand()/(RAND_MAX + 1.0); } bool nahodne_postaveni(int bile, int cerne) { enum {bila=1, cerna=2}; int sachovnice[8][8] = {{0}}; for (int b=1; b<=bile; b++) { int i; int j; do { i = int( 8*r() ); j = int( 8*r() ); } while (sachovnice[i][j] != 0); sachovnice[i][j] = bila; } for (int c=1; c<=cerne; c++) { int i; int j; do { i = int( 8*r() ); j = int( 8*r() ); } while (sachovnice[i][j] != 0); sachovnice[i][j] = cerna; } for (int radek=0; radek<8; radek++) for (int sloupec=0; sloupec<8; sloupec++) if (const int barva = sachovnice[radek][sloupec]) { for (int s=sloupec+1; s<8; s++) // postaveni vpravo if (const int sousedni = sachovnice[radek][s]) if (barva != sousedni) return false; else break; for (int s=sloupec-1; s>=0; s--) // postaveni vlevo if (const int sousedni = sachovnice[radek][s]) if (barva != sousedni) return false; else break; for (int r=radek+1; r<8; r++) // postaveni nad if (const int sousedni = sachovnice[r][sloupec]) if (barva != sousedni) return false; else break; for (int r=radek-1; r>=0; r--) // postaveni pod if (const int sousedni = sachovnice[r][sloupec]) if (barva != sousedni) return false; else break; // diagonala vlevo nahoru for (int r=radek-1, s=sloupec-1; r>=0 && s>=0; r--, s--) if (const int sousedni = sachovnice[r][s]) if (barva != sousedni) return false; else break; // diagonala vpravo nahoru for (int r=radek-1, s=sloupec+1; r>=0 && s<8; r--, s++) if (const int sousedni = sachovnice[r][s]) if (barva != sousedni) return false; else break; // diagonala vlevo dolu for (int r=radek+1, s=sloupec-1; r<8 && s>=0; r++, s--) if (const int sousedni = sachovnice[r][s]) if (barva != sousedni) return false; else break; // diagonala vpravo dolu for (int r=radek+1, s=sloupec+1; r<8 && s<8; r++, s++) if (const int sousedni = sachovnice[r][s]) if (barva != sousedni) return false; else break; } return true; } double simulace(int pokusy, int bile, int cerne) { int priznive_jevy = 0; for (int i=0; i<pokusy; i++) if (nahodne_postaveni(bile, cerne)) priznive_jevy++; return double(priznive_jevy)/pokusy; } int main() { std::cout.precision(2); std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); const int nexp = 1000000; std::srand(std::time(0)); for (int b=1; b<=4; b++) { for (int c=1; c<=b; c++) { std::cout << b << "/" << c << ": " << simulace(nexp, b, c) << " "; } std::cout << "\n"; } }
Varianta s testem vzájemně se ohrožujících dam podle Jiřího Bartoše.
#include <iostream> #include <cmath> #include <ctime> double r() { return rand()/(RAND_MAX + 1.0); } bool nahodne_postaveni(int bile, int cerne) { enum {bila=1, cerna=2}; int sachovnice[8][8] = {{0}}; int pocet = 0; int figury[64]; int figrad[64]; int figslp[64]; int pocet_barev[3]; pocet_barev[bila ] = bile; pocet_barev[cerna] = cerne; for (int barva=bila; barva<=cerna; barva++) for (int b=1; b<=pocet_barev[barva]; b++) { int i; int j; do { i = int( 8*r() ); j = int( 8*r() ); } while (sachovnice[i][j] != 0); figury[pocet] = sachovnice[i][j] = barva; figrad[pocet] = i; figslp[pocet] = j; pocet++; } for (int i=0; i<pocet; i++) for (int j=i+1; j<pocet; j++) if (figury[i] != figury[j]) { using std::abs; const int ri = figrad[i]; const int rj = figrad[j]; const int si = figslp[i]; const int sj = figslp[j]; if (ri == rj || si == sj) return false; // radky a sloupce if (abs(ri - rj) == abs(si - sj)) return false; // obe diagonaly } return true; } double simulace(int pokusy, int bile, int cerne) { int priznive_jevy = 0; for (int i=0; i<pokusy; i++) if (nahodne_postaveni(bile, cerne)) priznive_jevy++; return double(priznive_jevy)/pokusy; } int main() { std::cout.precision(2); std::cout.setf(std::ios_base::scientific, std::ios_base::floatfield); const int nexp = 1000000; std::srand(std::time(0)); for (int b=1; b<=4; b++) { for (int c=1; c<=b; c++) { std::cout << b << "/" << c << ": " << simulace(nexp, b, c) << " "; } std::cout << "\n"; } }
[ Zpět ]