#include<stdio.h> #include<time.h> #include<stdlib.h> #include<string.h> #include<vector> struct man{ char nick[55]; bool isSpy; int voted; }; man spy[105]; man nor[105]; int cntSpy, cntNor, oriNor, tarNor, leftNor; int cntTot; bool tarIsSpy; int tarVoted, tarVotedCnt, tarVotedPos; char tarNick[55]; void init(){ srand(time(NULL)); while(1){ man tmp; int isSpy; if(scanf("%s %d", tmp.nick, &isSpy)==EOF) break; if(isSpy == 1){ tmp.isSpy = true; spy[cntSpy++] = tmp; } else{ tmp.isSpy = false; nor[cntNor++] = tmp; } } oriNor = cntNor; leftNor = tarNor = (oriNor + 1) / 2; } int getRand(int maxVal){ return rand()%maxVal; } void voteAll(int voter, char nick[]){ int tar; int cnt = cntSpy + cntNor; int rnd; while( (rnd = getRand(cnt) ) == voter); if( rnd >= cntNor ){ spy[rnd-cntNor].voted++; if(tarVoted == spy[rnd-cntNor].voted) tarVotedCnt++; if(tarVoted < spy[rnd-cntNor].voted){ tarVotedCnt = 1; tarVoted = spy[rnd-cntNor].voted; tarVotedPos = rnd-cntNor; tarIsSpy = true; strcpy(tarNick,spy[rnd-cntNor].nick); } printf("%s -> %s",nick, spy[rnd-cntNor].nick); } else{ nor[rnd].voted++; if(tarVoted == nor[rnd].voted) tarVotedCnt++; if(tarVoted < nor[rnd].voted){ tarVotedCnt = 1; tarVoted = nor[rnd].voted; tarVotedPos = rnd; tarIsSpy = false; strcpy(tarNick,nor[rnd].nick); } printf("%s -> %s",nick, nor[rnd].nick); } } void voteNor(char nick[]){ int tar; int cnt = cntNor; int rnd = getRand(cnt); nor[rnd].voted++; if(tarVoted == nor[rnd].voted) tarVotedCnt++; if(tarVoted < nor[rnd].voted){ tarVotedCnt = 1; tarVoted = nor[rnd].voted; tarVotedPos = rnd; tarIsSpy = false; strcpy(tarNick,nor[rnd].nick); } printf("%s -> %s",nick, nor[rnd].nick); } int main(){ init(); int dayCnt = 0; while(1){ printf("%d번째 투표입니다.\n", ++dayCnt); for(int i=0;i<cntNor;i++){ nor[i].voted = 0; } for(int i=0;i<cntSpy;i++){ spy[i].voted = 0; } tarVotedCnt = 0; tarVoted = 0; tarVotedPos = -1; for(int i=0;i<cntNor;i++){ if(i!=0) printf(", "); voteAll(i,nor[i].nick); } for(int i=0;i<cntSpy;i++){ printf(", "); voteNor(spy[i].nick); } printf("\n"); printf("%s님: %d표", nor[0].nick, nor[0].voted); for(int i=1 ;i<cntNor;i++){ printf(", %s님: %d표", nor[i].nick, nor[i].voted); } for(int i=0;i<cntSpy;i++){ printf(", %s님: %d표", spy[i].nick, spy[i].voted); } printf("\n"); if(tarVotedCnt != 1){ printf("※ 동표가 발생하여 이번 투표는 무효로 합니다.\n"); } else{ printf("※ 다수결로 %s님을 처형했습니다.\n", tarNick); if(tarIsSpy == true){ printf("※ %s님은 스파이였습니다.\n",tarNick); printf("※ 일반군의 승리로 게임이 종료됩니다.\n"); break; } else{ for(int i = tarVotedPos; i<cntNor; i++){ nor[i] = nor[i+1]; } cntNor--; leftNor--; if(leftNor <= 0){ printf("※ 과반의 일반군(%d명)을 처형했습니다.\n", tarNor); printf("※ 스파이의 승리로 게임이 종료됩니다.\n"); break; } } } printf("\n\n"); } return 0; }
nand 1 gyeryak 1 woosungchoi 0 hyokhyok 1 idas4you 0 asbear 0 snow-airline 1 floridasnail 1 dyuryul 0 facemaker77 0 stellasjshin 0
1번째 투표입니다. woosungchoi -> stellasjshin, idas4you -> asbear, asbear -> floridasnail, dyuryul -> floridasnail, facemaker77 -> stellasjshin, stellasjshin -> woosungchoi, nand -> facemaker77, gyeryak -> idas4you, hyokhyok -> facemaker77, snow-airline -> asbear, floridasnail -> idas4you woosungchoi님: 1표, idas4you님: 2표, asbear님: 2표, dyuryul님: 0표, facemaker77님: 2표, stellasjshin님: 2표, nand님: 0표, gyeryak님: 0표, hyokhyok님: 0표, snow-airline님: 0표, floridasnail님: 2표 ※ 동표가 발생하여 이번 투표는 무효로 합니다. 2번째 투표입니다. woosungchoi -> nand, idas4you -> gyeryak, asbear -> woosungchoi, dyuryul -> asbear, facemaker77 -> asbear, stellasjshin -> asbear, nand -> dyuryul, gyeryak -> woosungchoi, hyokhyok -> asbear, snow-airline -> idas4you, floridasnail -> stellasjshin woosungchoi님: 2표, idas4you님: 1표, asbear님: 4표, dyuryul님: 1표, facemaker77님: 0표, stellasjshin님: 1표, nand님: 1표, gyeryak님: 1표, hyokhyok님: 0표, snow-airline님: 0표, floridasnail님: 0표 ※ 다수결로 asbear님을 처형했습니다. 3번째 투표입니다. woosungchoi -> floridasnail, idas4you -> stellasjshin, dyuryul -> stellasjshin, facemaker77 -> woosungchoi, stellasjshin -> gyeryak, nand -> stellasjshin, gyeryak -> dyuryul, hyokhyok -> dyuryul, snow-airline -> woosungchoi, floridasnail -> idas4you woosungchoi님: 2표, idas4you님: 1표, dyuryul님: 2표, facemaker77님: 0표, stellasjshin님: 3표, nand님: 0표, gyeryak님: 1표, hyokhyok님: 0표, snow-airline님: 0표, floridasnail님: 1표 ※ 다수결로 stellasjshin님을 처형했습니다. 4번째 투표입니다. woosungchoi -> gyeryak, idas4you -> dyuryul, dyuryul -> hyokhyok, facemaker77 -> dyuryul, nand -> woosungchoi, gyeryak -> woosungchoi, hyokhyok -> dyuryul, snow-airline -> woosungchoi, floridasnail -> woosungchoi woosungchoi님: 4표, idas4you님: 0표, dyuryul님: 3표, facemaker77님: 0표, nand님: 0표, gyeryak님: 1표, hyokhyok님: 1표, snow-airline님: 0표, floridasnail님: 0표 ※ 다수결로 woosungchoi님을 처형했습니다. ※ 과반의 일반군(3명)을 처형했습니다. ※ 스파이의 승리로 게임이 종료됩니다.