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. //freopen("input.txt","r",stdin);
  96. //freopen("output.txt","w",stdout);
  97.  
  98. init();
  99.  
  100. int dayCnt = 0;
  101. while(1){
  102. printf("%d번째 투표입니다.\n", ++dayCnt);
  103. for(int i=0;i<cntNor;i++){
  104. nor[i].voted = 0;
  105. }
  106. for(int i=0;i<cntSpy;i++){
  107. spy[i].voted = 0;
  108. }
  109. tarVotedCnt = 0;
  110. tarVoted = 0;
  111. tarVotedPos = -1;
  112.  
  113. for(int i=0;i<cntNor;i++){
  114. if(i!=0) printf(", ");
  115. voteAll(i,nor[i].nick);
  116. }
  117. for(int i=0;i<cntSpy;i++){
  118. printf(", ");
  119. voteNor(spy[i].nick);
  120. }
  121. printf("\n");
  122.  
  123. printf("%s님: %d표", nor[0].nick, nor[0].voted);
  124. for(int i=1 ;i<cntNor;i++){
  125. printf(", %s님: %d표", nor[i].nick, nor[i].voted);
  126. }
  127. for(int i=0;i<cntSpy;i++){
  128. printf(", %s님: %d표", spy[i].nick, spy[i].voted);
  129. }
  130. printf("\n");
  131.  
  132. if(tarVotedCnt != 1){
  133. printf("※ 동표가 발생하여 이번 투표는 무효로 합니다.\n");
  134. }
  135. else{
  136. printf("※ 다수결로 %s님을 처형했습니다.\n", tarNick);
  137.  
  138. if(tarIsSpy == true){
  139. printf("※ %s님은 스파이였습니다.\n",tarNick);
  140. printf("※ 일반군의 승리로 게임이 종료됩니다.\n");
  141. break;
  142. }
  143. else{
  144. for(int i = tarVotedPos; i<cntNor; i++){
  145. nor[i] = nor[i+1];
  146. }
  147. cntNor--;
  148. leftNor--;
  149. if(leftNor <= 0){
  150. printf("※ 과반의 일반군(%d명)을 처형했습니다.\n", tarNor);
  151. printf("※ 스파이의 승리로 게임이 종료됩니다.\n");
  152. break;
  153. }
  154. }
  155. }
  156.  
  157. printf("\n\n");
  158.  
  159. }
  160.  
  161.  
  162. return 0;
  163. }
  164.  
  165.  
  166.  
Success #stdin #stdout 0s 16080KB
stdin
soosoo 1
asbear 1
happadai 0
woosungchoi 0
snow-airline 1
zzrococo 1
gyeryak 0
nand 1
floridasnail 0
stdout
1번째 투표입니다.
happadai -> woosungchoi, woosungchoi -> snow-airline, gyeryak -> asbear, floridasnail -> happadai, soosoo -> floridasnail, asbear -> floridasnail, snow-airline -> woosungchoi, zzrococo -> woosungchoi, nand -> floridasnail
happadai님: 1표, woosungchoi님: 3표, gyeryak님: 0표, floridasnail님: 3표, soosoo님: 0표, asbear님: 1표, snow-airline님: 1표, zzrococo님: 0표, nand님: 0표
※ 동표가 발생하여 이번 투표는 무효로 합니다.


2번째 투표입니다.
happadai -> soosoo, woosungchoi -> gyeryak, gyeryak -> happadai, floridasnail -> woosungchoi, soosoo -> floridasnail, asbear -> gyeryak, snow-airline -> happadai, zzrococo -> gyeryak, nand -> happadai
happadai님: 3표, woosungchoi님: 1표, gyeryak님: 3표, floridasnail님: 1표, soosoo님: 1표, asbear님: 0표, snow-airline님: 0표, zzrococo님: 0표, nand님: 0표
※ 동표가 발생하여 이번 투표는 무효로 합니다.


3번째 투표입니다.
happadai -> gyeryak, woosungchoi -> snow-airline, gyeryak -> snow-airline, floridasnail -> nand, soosoo -> gyeryak, asbear -> happadai, snow-airline -> gyeryak, zzrococo -> floridasnail, nand -> floridasnail
happadai님: 1표, woosungchoi님: 0표, gyeryak님: 3표, floridasnail님: 2표, soosoo님: 0표, asbear님: 0표, snow-airline님: 2표, zzrococo님: 0표, nand님: 1표
※ 다수결로 gyeryak님을 처형했습니다.


4번째 투표입니다.
happadai -> snow-airline, woosungchoi -> zzrococo, floridasnail -> zzrococo, soosoo -> happadai, asbear -> woosungchoi, snow-airline -> happadai, zzrococo -> woosungchoi, nand -> woosungchoi
happadai님: 2표, woosungchoi님: 3표, floridasnail님: 0표, soosoo님: 0표, asbear님: 0표, snow-airline님: 1표, zzrococo님: 2표, nand님: 0표
※ 다수결로 woosungchoi님을 처형했습니다.
※ 과반의 일반군(2명)을 처형했습니다.
※ 스파이의 승리로 게임이 종료됩니다.