fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // #define MY_FILE "test.txt"
  4. #define MY_FILE "c122-875.c.in"
  5.  
  6. int sum1(),
  7. sum1x(),
  8. gyo, retu,
  9. i,
  10. j,
  11. c,
  12. a,
  13. b,
  14. *matrix,
  15. candidates,
  16. kae();
  17.  
  18. void pp(int *a, int n)
  19. {
  20. while (n--) {
  21. printf("%d ", *a++);
  22. if (n % retu == 0)
  23. putchar('\n');
  24. }
  25. printf("\n\n");
  26. }
  27.  
  28. int main()
  29. {
  30. FILE *fp;
  31.  
  32. fp = fopen(MY_FILE, "r");
  33.  
  34. fscanf(fp, "%*[^=]=%d", &retu);
  35. fscanf(fp, "%*[^=]=%d", &gyo);
  36. candidates = retu;
  37.  
  38. printf("CANDIDATES=%d,VOTES=%d\n", retu, gyo);
  39.  
  40. matrix = (int *)malloc(sizeof(int) * gyo * retu);
  41. if (matrix == NULL) exit(1);
  42.  
  43. {
  44. int *p = matrix,
  45. i = gyo * retu;
  46.  
  47. while (i--) {
  48. fscanf(fp, "%d", p);
  49. printf("%d ", *p);
  50. ++p;
  51. if (i % retu == 0)
  52. putchar('\n');
  53. }
  54. }
  55. sum1();
  56. fclose(fp);
  57. free(matrix);
  58.  
  59. return 0;
  60. }
  61.  
  62. //--------------------------集計-----------------------
  63. int sum1()
  64. {
  65. int i,
  66. mini,
  67. subTotal[retu + 1];
  68.  
  69. while (1) {
  70. // pp(matrix, gyo * retu);
  71. // subTotal calc
  72. for (i = 0; i <= retu; ++i)
  73. subTotal[i] = 0;
  74. for (i = 0; i < gyo * retu; i += retu)
  75. subTotal[matrix[i]]++;
  76.  
  77. for (i = 1; i <= retu; ++i) {
  78. // printf("sub %d %d %d %d %d\n", i, subTotal[i], gyo, retu, candidates);
  79. if (subTotal[i] > gyo / 2 ) { /* majority found */
  80. printf("------当選者-----\nCANDIDATE%d\n", i );
  81. return 0;
  82. }
  83. }
  84. // find minimum
  85. mini = 1;
  86. for (i = 2; i <= retu; ++i)
  87. if (subTotal[i] < subTotal[mini])
  88. mini = i;
  89. printf("最少候補番号 = %d\n", mini);
  90. // delete minimum and compress matrix
  91. {
  92. int *p1,
  93. *p2;
  94. p1 = p2 = matrix;
  95. for ( i = gyo * retu; i; --i) {
  96. *p1 = *p2;
  97. if (*p2 != mini)
  98. ++p1;
  99. ++p2;
  100. }
  101. }
  102. --retu;
  103. }
  104.  
  105. return 0 ;
  106. }
  107.  
Runtime error #stdin #stdout 0.01s 2808KB
stdin
Standard input is empty
stdout
Standard output is empty