1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;
const int period=20;
const int rand_max=period-1;
int seq[period]={0,1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,19};
int seed=0;
int draw();
int draw_calls=0;
int main()
{
ofstream fout("out.txt");
ifstream fin("in.txt");
const int n=11;
int freq[n]={0};
int i;
int start;
int stop;
fout << "way number 1...\n" << endl;
start=clock();
for (i=0; i<100000*period; i++)
freq[draw()%n]++;
stop=clock();
for (i=0; i<n; i++)
fout << i << " -> " << freq[i] << " time(s)" << endl;
fout << "\ntime taken: " << (1000*(stop-start))/CLOCKS_PER_SEC << " ms" << endl;
fout << "draw calls: " << draw_calls << endl;
for (i=0; i<n; i++) freq[i]=0;
draw_calls=0;
fout << "\nway number 2...\n" << endl;
int total=0;
int temp;
int count=0;
bool redraw;
start=clock();
for (i=0; i<100000*period; i++)
{
while (true)
{
temp=draw()%n;
count=0;
redraw=false;
while (freq[temp]>1000+total/n)
{
temp+=++count;
temp%=n;
if (count==10+n/4) {redraw=true; break;}
}
if (redraw) continue;
freq[temp]++;
total++;
break;
}
}
stop=clock();
for (i=0; i<n; i++)
fout << i << " -> " << freq[i] << " time(s)" << endl;
fout << "\ntime taken: " << (1000*(stop-start))/CLOCKS_PER_SEC << " ms" << endl;
fout << "draw calls: " << draw_calls << endl;
fout << "\ncustom frequencies...\n" << endl;
double target_freq[n];
for (i=0; i<n; i++) target_freq[i]=0.0;
double total_freq=0;
bool problem=false;
for (i=0; i<n; i++)
{
fin >> target_freq[i];
if (!fin) break;
total_freq+=target_freq[i];
}
fout << "target frequencies:\n";
for (i=0; i<n; i++) fout << target_freq[i] << endl;
fout << endl;
if ( fabs(total_freq-1)>0.000001 ) problem=true;
if (problem) {fout << "error reading from input file... total_freq=="
<< total_freq<< "..." << endl; return 0;}
for (i=0; i<n; i++) freq[i]=0;
draw_calls=0;
total=0;
start=clock();
for (i=0; i<100000*period; i++)
{
while (true)
{
temp=draw()%n;
count=0;
redraw=false;
while (freq[temp]>1000+target_freq[temp]*total)
{
temp+=++count;
temp%=n;
if (count==10+n/4) {redraw=true; break;}
}
if (redraw) continue;
freq[temp]++;
total++;
break;
}
}
stop=clock();
for (i=0; i<n; i++)
fout << i << " -> " << freq[i] << " time(s)" << endl;
fout << "\ntime taken: " << (1000*(stop-start))/CLOCKS_PER_SEC << " ms" << endl;
fout << "draw calls: " << draw_calls << endl;
return 0;
}
int draw()
{
draw_calls++;
if (seed>rand_max) seed=0;
return seq[seed++];
}
|