#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// prototype
void pp(char *, int , int );
int searchMajority();
void votesExchange( int );
char *matrix;
int votes,
candidates,
*sum;
void pp(char *a, int x, int y)
{
int i,
j;
for ( i = 1; i <= x; ++i) {
for ( j = 1; j <= y; ++j) {
}
}
}
int searchMajority()
{
int i;
char *p = matrix;
// 機能1:
// sumに集計結果をセットする 。
for ( i = 0; i < candidates; ++i) {
sum[i] = 0;
}
for ( i = 0; i < votes; ++i) {
// printf("matrix %c\n", *p);
sum[*p - '1']++;
p += candidates;
}
// for ( i = 0; i < candidates; ++i) {
// printf("sum[%d] = %d\n", i, sum[i]);
// }
// 機能2:
// 過半数獲得候補者が有れば、候補者番号(1~candidates)を返す。
// 過半数候補者が無ければ、0を返す。
for (i = 0; i < candidates; ++i) {
if (sum[i] > votes / 2) {
return i;
}
}
return 0;
}
void votesExchange(int n)
{
char t,
*p = matrix;
int i,
min = votes;
for ( i = 1; i < candidates; ++i) {
if ((min > sum[i]) && (sum[i])) {
min = sum[i];
}
}
for ( i = 0; i < votes; ++i) {
if (sum[*p - '1'] == min) {
// swap
t = *p;
*p = *(p + n);
*(p + n) = t;
}
p += candidates;
}
}
int main()
{
FILE *fp;
char c,
*p,
buf[100],
// s[100],
fname[] = "c672-3.c.in";
int i,
n,/* 入替対象列 */
max,/* 最大得票数 */
majority = 0;/* 過半数を獲得した候補者番号 */
// 入力ファイルopen
if ( fp == NULL ) {
printf( "%sファイルが開けません\n", fname
); return -1;
}
// 候補者数をファイルから読み込む
if (p == NULL) {
return -1;
}
candidates
= atoi(p
+ 1);
// 投票者数をファイルから読み込む
if (p == NULL) {
return -1;
}
// 集計エリア確保
matrix
= (char *)malloc(sizeof(char) * candidates
* votes
); sum
= (int *)malloc(sizeof(int) * candidates
);
// ファイルからデータ読込
p = matrix;
i = votes * candidates;
while ((c
= fgetc(fp
)) != EOF
) { if (c != '\n') {
*p++ = c;
if (--i == 0) {
break;
}
}
}
n = 1;
majority = searchMajority();
while (!majority && (n < candidates)) {
votesExchange(n);
majority = searchMajority();
++n;
}
if (majority) { /* 入替による過半数が有った場合 */
printf("majority = %d\n", majority
); } else { /* 入替による過半数が無かった場合、最大得票数の候補を探す(複数の可能性あり) */
max = sum[0];/* 最大得票数を探す */
for (i = 0; i < candidates; ++i) {
if (max < sum[i]) {
max = sum[i];
}
}
for (i = 0; i < candidates; ++i) { /* 最大得票数に一致する候補者番号を出力する */
if (sum[i] == max) {
}
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKLy8gcHJvdG90eXBlCnZvaWQgcHAoY2hhciAqLCBpbnQgLCBpbnQgKTsKaW50IHNlYXJjaE1ham9yaXR5KCk7CnZvaWQgdm90ZXNFeGNoYW5nZSggaW50ICk7CgpjaGFyICptYXRyaXg7CmludCB2b3RlcywKICAgIGNhbmRpZGF0ZXMsCiAgICAqc3VtOwoKdm9pZCBwcChjaGFyICphLCBpbnQgeCwgaW50IHkpCnsKICBpbnQgaSwKICAgICAgajsKCiAgZm9yICggaSA9IDE7IGkgPD0geDsgKytpKSB7CiAgICBmb3IgKCBqID0gMTsgaiA8PSB5OyArK2opIHsKICAgICAgcHV0Y2hhcigqYSsrKTsKICAgIH0KICAgIHB1dHMoIiIpOwogIH0KfQoKaW50IHNlYXJjaE1ham9yaXR5KCkKewogIGludCBpOwogIGNoYXIgKnAgPSBtYXRyaXg7CgogIC8vIOapn+iDve+8ke+8mgogIC8vIHN1beOBq+mbhuioiOe1kOaenOOCkuOCu+ODg+ODiOOBmeOCiyDjgIIKICBmb3IgKCBpID0gMDsgaSA8IGNhbmRpZGF0ZXM7ICsraSkgewogICAgc3VtW2ldID0gMDsKICB9CiAgZm9yICggaSA9IDA7IGkgPCB2b3RlczsgKytpKSB7CiAgICAvLyBwcmludGYoIm1hdHJpeCAlY1xuIiwgKnApOwogICAgc3VtWypwIC0gJzEnXSsrOwogIC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IEZJTEUgKmZwOwogIGNoYXIgYywKICAgICAgICpwLAogICAgICAgYnVmWzEwMF0sCiAgICAgICAvLyBzWzEwMF0sCiAgICAgICBmbmFtZVtdID0gImM2NzItMy5jLmluIjsKICBpbnQgaSwKICAgICAgbiwvKiDlhaXmm7/lr77osaHliJcgKi8KICAgICAgbWF4LC8qIOacgOWkp+W+l+elqOaVsCAqLwogICAgICBtYWpvcml0eSA9IDA7Lyog6YGO5Y2K5pWw44KS542y5b6X44GX44Gf5YCZ6KOc6ICF55Wq5Y+3ICovCgogIC8vIOWFpeWKm+ODleOCoeOCpOODq29wZW4KICBmcCAgICAgPSBmb3BlbihmbmFtZSwgInIiKTsKICBpZiAoIGZwID09IE5VTEwgKSB7CiAgICBwcmludGYoICIlc+ODleOCoeOCpOODq+OBjOmWi+OBkeOBvuOBm+OCk1xuIiwgZm5hbWUgKTsKICAgIHJldHVybiAtMTsKICB9CgogIC8vIOWAmeijnOiAheaVsOOCkuODleOCoeOCpOODq+OBi+OCieiqreOBv+i+vOOCgAogIGZnZXRzKGJ1ZiwgMTAwLCBmcCk7CiAgcCA9IHN0cmNocihidWYsICc9Jyk7CiAgaWYgKHAgPT0gTlVMTCkgewogICAgcHJpbnRmKCLlgJnoo5zogIXmlbDjgpLoqq3jgb/ovrzjgoHjgb7jgZvjgpNcbiIgKTsKICAgIHJldHVybiAtMTsKICB9CiAgY2FuZGlkYXRlcyA9IGF0b2kocCArIDEpOwoKICAvLyDmipXnpajogIXmlbDjgpLjg5XjgqHjgqTjg6vjgYvjgonoqq3jgb/ovrzjgoAKICBmZ2V0cyhidWYsIDEwMCwgZnApOwogIHAgPSBzdHJjaHIoYnVmLCAnPScpOwogIGlmIChwID09IE5VTEwpIHsKICAgIHByaW50Zigi5oqV56Wo6ICF5pWw44KS6Kqt44G/6L6844KB44G+44Gb44KTXG4iICk7CiAgICByZXR1cm4gLTE7CiAgfQogIHZvdGVzID0gYXRvaShwICsgMSk7CgogIC8vIOmbhuioiOOCqOOD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