-
Notifications
You must be signed in to change notification settings - Fork 1
/
pills.cpp
78 lines (65 loc) · 1.62 KB
/
pills.cpp
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
// Jeff Ruhl
// Poison Pills
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
using namespace std;
typedef queue<bool> PillsQueue;
typedef queue<int> CaptivesQueue;
typedef priority_queue<int, vector<int>, greater<int>> DeadmanQueue;
bool is_deadly( char pill_name ) {
return ('B' == pill_name ? true : false);
}
int main( int argc, char* argv[] ) {
ifstream fin( "pills.in" );
if ( fin ) {
ofstream fout("pills.out");
int pill_count = 0, captive_count = 0;
fin >> pill_count;
fin >> captive_count;
while ( 0 != pill_count && 0 != captive_count ) {
PillsQueue pills;
CaptivesQueue captives;
DeadmanQueue dead_men;
char pill_name;
for ( int i = 0; i < pill_count; i++ ) {
fin >> pill_name;
pills.push( is_deadly( pill_name ) );
}
for ( int i = 1; i <= captive_count; i++ ) {
captives.push( i );
}
while ( !captives.empty() ) {
int captive = captives.front(); captives.pop();
// put back the first pill
pills.push( pills.front() );
pills.pop();
// the next determines their fate
if ( pills.front() ) {
dead_men.push( captive );
}
pills.pop();
}
if ( dead_men.empty() ) {
fout << 0;
}
while ( !dead_men.empty() ) {
fout << dead_men.top(); dead_men.pop();
// the last dead man gets no space
if ( !dead_men.empty() ) {
fout << " ";
}
}
fout << endl;
fin >> pill_count;
fin >> captive_count;
}
fout.close();
fin.close();
}
else {
cout << "Could not open file 'pills.in'" << endl;
}
return 0;
}