#include <stdio.h>
#include <stdlib.h>
// #define MY_FILE "test.txt"
#define MY_FILE "c122-875.c.in"
int sum1(),
sum1x(),
gyo, retu,
i,
j,
c,
a,
b,
*matrix,
candidates,
kae();
void pp(int *a, int n)
{
while (n--) {
printf("%d ", *a++);
if (n % retu == 0)
putchar('\n');
}
printf("\n\n");
}
int main()
{
FILE *fp;
fp = fopen(MY_FILE, "r");
fscanf(fp, "%*[^=]=%d", &retu);
fscanf(fp, "%*[^=]=%d", &gyo);
candidates = retu;
printf("CANDIDATES=%d,VOTES=%d\n", retu, gyo);
matrix = (int *)malloc(sizeof(int) * gyo * retu);
if (matrix == NULL) exit(1);
{
int *p = matrix,
i = gyo * retu;
while (i--) {
fscanf(fp, "%d", p);
printf("%d ", *p);
++p;
if (i % retu == 0)
putchar('\n');
}
}
sum1();
fclose(fp);
free(matrix);
return 0;
}
//--------------------------集計-----------------------
int sum1()
{
int i,
mini,
subTotal[retu + 1];
while (1) {
// pp(matrix, gyo * retu);
// subTotal calc
for (i = 0; i <= retu; ++i)
subTotal[i] = 0;
for (i = 0; i < gyo * retu; i += retu)
subTotal[matrix[i]]++;
for (i = 1; i <= retu; ++i) {
// printf("sub %d %d %d %d %d\n", i, subTotal[i], gyo, retu, candidates);
if (subTotal[i] > gyo / 2 ) { /* majority found */
printf("------当選者-----\nCANDIDATE%d\n", i );
return 0;
}
}
// find minimum
mini = 1;
for (i = 2; i <= retu; ++i)
if (subTotal[i] < subTotal[mini])
mini = i;
printf("最少候補番号 = %d\n", mini);
// delete minimum and compress matrix
{
int *p1,
*p2;
p1 = p2 = matrix;
for ( i = gyo * retu; i; --i) {
*p1 = *p2;
if (*p2 != mini)
++p1;
++p2;
}
}
--retu;
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KLy8gI2RlZmluZSBNWV9GSUxFICJ0ZXN0LnR4dCIKI2RlZmluZSBNWV9GSUxFICJjMTIyLTg3NS5jLmluIgo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CnsKICBpbnQgaSwKICAgICAgbWluaSwKICAgICAgc3ViVG90YWxbcmV0dSArIDFdOwoKICB3aGlsZSAoMSkgewogICAgLy8gcHAobWF0cml4LCBneW8gKiByZXR1KTsKICAgIC8vIHN1YlRvdGFsIGNhbGMKICAgIGZvciAoaSA9IDA7IGkgPD0gcmV0dTsgKytpKQogICAgICBzdWJUb3RhbFtpXSA9IDA7CiAgICBmb3IgKGkgPSAwOyBpIDwgZ3lvICogcmV0dTsgaSArPSByZXR1KQogICAgICBzdWJUb3RhbFttYXRyaXhbaV1dKys7CgogICAgZm9yIChpID0gMTsgaSA8PSByZXR1OyArK2kpIHsKICAgICAgLy8gcHJpbnRmKCJzdWIgJWQgJWQgJWQgJWQgJWRcbiIsIGksIHN1YlRvdGFsW2ldLCBneW8sIHJldHUsIGNhbmRpZGF0ZXMpOwogICAgICBpZiAoc3ViVG90YWxbaV0gPiBneW8gLyAyICkgeyAvKiBtYWpvcml0eSBmb3VuZCAqLwogICAgICAgIHByaW50ZigiLS0tLS0t5b2T6YG46ICFLS0tLS1cbkNBTkRJREFURSVkXG4iLCBpICk7CiAgICAgICAgcmV0dXJuIDA7CiAgICAgIH0KICAgIH0KICAgIC8vIGZpbmQgbWluaW11bQogICAgbWluaSA9IDE7CiAgICBmb3IgKGkgPSAyOyBpIDw9IHJldHU7ICsraSkKICAgICAgaWYgKHN1YlRvdGFsW2ldIDwgc3ViVG90YWxbbWluaV0pCiAgICAgICAgbWluaSA9IGk7CiAgICBwcmludGYoIuacgOWwkeWAmeijnOeVquWPtyA9ICVkXG4iLCBtaW5pKTsKICAgIC8vIGRlbGV0ZSBtaW5pbXVtIGFuZCBjb21wcmVzcyBtYXRyaXgKICAgIHsKICAgICAgaW50ICpwMSwKICAgICAgICAgICpwMjsKICAgICAgcDEgPSBwMiA9IG1hdHJpeDsKICAgICAgZm9yICggaSA9IGd5byAqIHJldHU7IGk7IC0taSkgewogICAgICAgICpwMSA9ICpwMjsKICAgICAgICBpZiAoKnAyICE9IG1pbmkpCiAgICAgICAgICArK3AxOwogICAgICAgICsrcDI7CiAgICAgIH0KICAgIH0KICAgIC0tcmV0dTsKICB9CgogIHJldHVybiAwIDsKfQo=