-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path1115(Simulation)
214 lines (208 loc) · 5.1 KB
/
1115(Simulation)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct question{
string code;
string name;
int numOfBranch;
char branch[15];
int branchIdx[256];
string branchDes[15];
} questions[110];
void print(int n, int ws){
int kg = ws-1;
if(n>=10) kg--;
if(n>=100) kg--;
if(n>=1000) kg--;
if(n>=10000) kg--;
for(int i = 0; i < kg; i++) cout << " ";
cout << n;
}
ostream& operator<<(ostream& out, const question& q){
out << q.code << " " << q.name << endl;
for(int i = 0; i < q.numOfBranch; i++){
out << " " << q.branch[i] << " " << q.branchDes[i] << endl;
}
return out;
}
void get(string &s){
s = "";
while(s.length() == 0) getline(cin, s);
}
int cnt = -1;
int main() {
string surveyName;
get(surveyName);
//int branchCnt;
map<string, int> questionIdx;
while(1){
string temp;
get(temp);
if(temp[0] == '#'){
cnt++;
break;
}
else if(temp[0] == ' '){
int branchCnt = questions[cnt].numOfBranch;
questions[cnt].branch[branchCnt] = temp[1];
questions[cnt].branchDes[branchCnt] = temp.substr(3);
questions[cnt].branchIdx[temp[1]] = branchCnt;
questions[cnt].numOfBranch++;
}
else{
cnt++;
questions[cnt].code = temp.substr(0,3);
questions[cnt].name = temp.substr(4);
questions[cnt].numOfBranch = 0;
questionIdx.insert(pair<string, int>(questions[cnt].code, cnt));
//cout << "question code: " << questions[cnt].code << " ,question number: " << cnt << endl;
}
}
int ansCnt = 0;
string ans[10010];
string tempAns;
while(1){
get(tempAns);
if(tempAns == "#") break;
ans[ansCnt] = tempAns;
ansCnt++;
}
string query;
while(1){
get(query);
if(query == "#") break;
int firstQ = questionIdx[query.substr(0,3)];
int secondQ = questionIdx[query.substr(4,3)];
//cout << firstQ << secondQ << endl;
string queryName = query.substr(8);
cout << surveyName << " - " << queryName << endl;
cout << questions[firstQ] << questions[secondQ] << endl;
int gs[15][15] = {0};
int cnt1 = questions[firstQ].numOfBranch, cnt2 = questions[secondQ].numOfBranch;
for(int i = 0; i < ansCnt; i++){
int x1 = questions[firstQ].branchIdx[ans[i][firstQ]];
int x2 = questions[secondQ].branchIdx[ans[i][secondQ]];
gs[x1][x2]++;
}
int gs1[15] = {0}, gs2[15] = {0};
for(int i = 0; i < cnt1; i++){
for(int j = 0; j < cnt2; j++){
gs1[i] += gs[i][j];
gs2[j] += gs[i][j];
}
}
int bfs1[15][15], bfs2[15][15];
for(int i = 0; i < cnt1; i++){
if(gs1[i] == 0){
for(int j = 0; j <= cnt2; j++) bfs1[i][j] = -1;
}
else{
bfs1[i][cnt2] = 100;
bool kj[15] = {0};
for(int j = 0; j < cnt2; j++){
bfs1[i][j] = (gs[i][j]*100)/gs1[i];
if((gs[i][j]*100)%gs1[i] != 0) kj[j] = 1;
}
int sum = 0;
for(int j = 0; j < cnt2; j++) sum += bfs1[i][j];
int cha = 100-sum;
for(int j = 0; j < cnt2; j++){
if(cha == 0) break;
if(!kj[j]) continue;
bfs1[i][j]++;
cha--;
}
}
}
bfs1[cnt1][cnt2] = 100;
bool kjk[15] = {0};
for(int j = 0; j < cnt2; j++){
bfs1[cnt1][j] = (gs2[j]*100)/ansCnt;
if((gs2[j]*100)%ansCnt != 0) kjk[j] = 1;
}
int summ = 0;
for(int j = 0; j < cnt2; j++) summ += bfs1[cnt1][j];
int chaa = 100-summ;
for(int j = 0; j < cnt2; j++){
if(chaa == 0) break;
if(!kjk[j]) continue;
bfs1[cnt1][j]++;
chaa--;
}
for(int j = 0; j < cnt2; j++){
if(gs2[j] == 0) {
for(int i = 0; i <= cnt1; i++) bfs2[i][j] = -1;
}
else{
bfs2[cnt1][j] = 100;
bool kj[15] = {0};
for(int i = 0; i < cnt1; i++){
bfs2[i][j] = (gs[i][j]*100)/gs2[j];
if((gs[i][j]*100)%gs2[j] != 0) kj[i]=1;
}
int sum = 0;
for(int i = 0; i < cnt1; i++) sum += bfs2[i][j];
int cha = 100-sum;
for(int i = 0; i < cnt1; i++){
if(cha == 0) break;
if(!kj[i]) continue;
bfs2[i][j]++;
cha--;
}
}
}
bfs2[cnt1][cnt2] = 100;
bool kjj[15] = {0};
for(int i = 0; i < cnt1; i++){
bfs2[i][cnt2] = (gs1[i]*100)/ansCnt;
if((gs1[i]*100)%ansCnt != 0) kjj[i] = 1;
}
int sumu = 0;
for(int i = 0; i < cnt1; i++) sumu += bfs2[i][cnt2];
int chah = 100-sumu;
for(int i = 0; i < cnt1; i++){
if(chah == 0) break;
if(!kjj[i]) continue;
bfs2[i][cnt2]++;
chah--;
}
for(int i = 0; i < cnt1; i++) gs[i][cnt2] = gs1[i];
for(int j = 0; j < cnt2; j++) gs[cnt1][j] = gs2[j];
gs[cnt1][cnt2] = ansCnt;
string code1 = questions[firstQ].code, code2 = questions[secondQ].code;
cout << " ";
for(int j = 0; j < cnt2; j++) cout << " " << code2 << ":" << questions[secondQ].branch[j];
cout << " TOTAL";
cout << endl;
for(int i = 0; i <= cnt1; i++){
if(i != cnt1) cout << " " << code1 << ":" << questions[firstQ].branch[i];
else cout << " TOTAL";
for(int j = 0; j <= cnt2; j++){
print(gs[i][j], 6);
}
cout << endl;
cout << " ";
for(int j = 0; j <= cnt2; j++){
if(bfs1[i][j] == -1) cout << " -";
else {
print(bfs1[i][j], 5);
cout << "%";
}
}
cout << endl;
cout << " ";
for(int j = 0; j <= cnt2; j++){
if(bfs2[i][j] == -1) cout << " -";
else {
print(bfs2[i][j], 5);
cout << "%";
}
}
cout << endl;
}
cout << endl;
}
return 0;
}