fork download
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<vector>
  6.  
  7. struct man{
  8. char nick[55];
  9. bool isSpy;
  10. int voted;
  11. };
  12.  
  13. man spy[105];
  14. man nor[105];
  15. int cntSpy, cntNor, oriNor, tarNor, leftNor;
  16. int cntTot;
  17.  
  18. bool tarIsSpy;
  19. int tarVoted, tarVotedCnt, tarVotedPos;
  20. char tarNick[55];
  21.  
  22. void init(){
  23.  
  24. srand(time(NULL));
  25.  
  26. while(1){
  27. man tmp; int isSpy;
  28. if(scanf("%s %d", tmp.nick, &isSpy)==EOF) break;
  29. if(isSpy == 1){
  30. tmp.isSpy = true;
  31. spy[cntSpy++] = tmp;
  32. }
  33. else{
  34. tmp.isSpy = false;
  35. nor[cntNor++] = tmp;
  36. }
  37. }
  38.  
  39. oriNor = cntNor;
  40. leftNor = tarNor = (oriNor + 1) / 2;
  41. }
  42.  
  43. int getRand(int maxVal){
  44. return rand()%maxVal;
  45. }
  46.  
  47. void voteAll(int voter, char nick[]){
  48. int tar;
  49. int cnt = cntSpy + cntNor;
  50. int rnd;
  51. while( (rnd = getRand(cnt) ) == voter);
  52. if( rnd >= cntNor ){
  53. spy[rnd-cntNor].voted++;
  54. if(tarVoted == spy[rnd-cntNor].voted) tarVotedCnt++;
  55. if(tarVoted < spy[rnd-cntNor].voted){
  56. tarVotedCnt = 1;
  57. tarVoted = spy[rnd-cntNor].voted;
  58. tarVotedPos = rnd-cntNor;
  59. tarIsSpy = true;
  60. strcpy(tarNick,spy[rnd-cntNor].nick);
  61. }
  62. printf("%s -> %s",nick, spy[rnd-cntNor].nick);
  63. }
  64. else{
  65. nor[rnd].voted++;
  66. if(tarVoted == nor[rnd].voted) tarVotedCnt++;
  67. if(tarVoted < nor[rnd].voted){
  68. tarVotedCnt = 1;
  69. tarVoted = nor[rnd].voted;
  70. tarVotedPos = rnd;
  71. tarIsSpy = false;
  72. strcpy(tarNick,nor[rnd].nick);
  73. }
  74. printf("%s -> %s",nick, nor[rnd].nick);
  75. }
  76. }
  77.  
  78. void voteNor(char nick[]){
  79. int tar;
  80. int cnt = cntNor;
  81. int rnd = getRand(cnt);
  82. nor[rnd].voted++;
  83. if(tarVoted == nor[rnd].voted) tarVotedCnt++;
  84. if(tarVoted < nor[rnd].voted){
  85. tarVotedCnt = 1;
  86. tarVoted = nor[rnd].voted;
  87. tarVotedPos = rnd;
  88. tarIsSpy = false;
  89. strcpy(tarNick,nor[rnd].nick);
  90. }
  91. printf("%s -> %s",nick, nor[rnd].nick);
  92. }
  93.  
  94. int main(){
  95. init();
  96.  
  97. int dayCnt = 0;
  98. while(1){
  99. printf("%d번째 투표입니다.\n", ++dayCnt);
  100. for(int i=0;i<cntNor;i++){
  101. nor[i].voted = 0;
  102. }
  103. for(int i=0;i<cntSpy;i++){
  104. spy[i].voted = 0;
  105. }
  106. tarVotedCnt = 0;
  107. tarVoted = 0;
  108. tarVotedPos = -1;
  109.  
  110. for(int i=0;i<cntNor;i++){
  111. if(i!=0) printf(", ");
  112. voteAll(i,nor[i].nick);
  113. }
  114. for(int i=0;i<cntSpy;i++){
  115. printf(", ");
  116. voteNor(spy[i].nick);
  117. }
  118. printf("\n");
  119.  
  120. printf("%s님: %d표", nor[0].nick, nor[0].voted);
  121. for(int i=1 ;i<cntNor;i++){
  122. printf(", %s님: %d표", nor[i].nick, nor[i].voted);
  123. }
  124. for(int i=0;i<cntSpy;i++){
  125. printf(", %s님: %d표", spy[i].nick, spy[i].voted);
  126. }
  127. printf("\n");
  128.  
  129. if(tarVotedCnt != 1){
  130. printf("※ 동표가 발생하여 이번 투표는 무효로 합니다.\n");
  131. }
  132. else{
  133. printf("※ 다수결로 %s님을 처형했습니다.\n", tarNick);
  134.  
  135. if(tarIsSpy == true){
  136. printf("※ %s님은 스파이였습니다.\n",tarNick);
  137. printf("※ 일반군의 승리로 게임이 종료됩니다.\n");
  138. break;
  139. }
  140. else{
  141. for(int i = tarVotedPos; i<cntNor; i++){
  142. nor[i] = nor[i+1];
  143. }
  144. cntNor--;
  145. leftNor--;
  146. if(leftNor <= 0){
  147. printf("※ 과반의 일반군(%d명)을 처형했습니다.\n", tarNor);
  148. printf("※ 스파이의 승리로 게임이 종료됩니다.\n");
  149. break;
  150. }
  151. }
  152. }
  153.  
  154. printf("\n\n");
  155.  
  156. }
  157.  
  158.  
  159. return 0;
  160. }
  161.  
  162.  
  163.  
Success #stdin #stdout 0s 15256KB
stdin
nand 1
gyeryak 1
woosungchoi 0
hyokhyok 1
idas4you 0
asbear 0
snow-airline 1
floridasnail 1
dyuryul 0
facemaker77 0
stellasjshin 0
stdout
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명)을 처형했습니다.
※ 스파이의 승리로 게임이 종료됩니다.