C
#include <stdio.h>
#define NCANDIDATES 4
static const char * const cand_list[NCANDIDATES] = {
    "Alberto Arbusto",
    "Juan Perez",
    "Mickey Mouse",
    "Jorge Sangre"
};
#define BUFFER_SIZE 100
int
main(int argc, char **argv)
{
    int votes[NCANDIDATES];
    int candidate;
    size_t name_start;
    int i;
    int j;
    int place;
    int max;
    size_t bytes;
    char buffer[BUFFER_SIZE];
    /*
    Make sure input is read in text mode, so we don't have to
    worry about whether line endings are LF or CRLF.
    */
    freopen(NULL, "rt", stdin);
    /* Initialize vote tally. */
    for (candidate = 0; candidate < NCANDIDATES; candidate++) {
        votes[candidate] = 0;
    }
    /* Read and process vote file. */
    do {
        /* Read a block of data. */
        bytes = fread(buffer, 1, BUFFER_SIZE, stdin);
        /* Loop over the data, finding and counting the votes. */
        name_start = 0;
        for (i = 0; i < bytes; i++) {
            if (buffer[i] == '\n') {
                /* Found name. */
                buffer[i] = '\0'; // nul-terminate name so strcmp will work
                /* Look up candidate. */
                for (j = 0; j < NCANDIDATES; j++) {
                    if (strcmp(&buffer[name_start], cand_list[j]) == 0) {
                        candidate = j;
                        break;
                    }
                }
                /* Count vote. */
                ++votes[candidate];
                /* Next name starts at next character */
                name_start = i + 1;
            }
        }
    } while (bytes > 0);
    /* Output the candidates, in decreasing order of votes. */
    for (place = 0; place < NCANDIDATES; place++) {
        max = -1;
        for (j = 0; j < NCANDIDATES; j++) {
            if (votes[j] > max) {
                candidate = j;
                max = votes[j];
            }
        }
        printf("%8d %s\n", votes[candidate], cand_list[candidate]);
        votes[candidate] = -1; // Remove from consideration for next place.
    }
    return 0;
}
青睐Jorge Sangre。
在使用随机生成的投票文件进行测试时,即使Alberto Arbusto实际获得的选票最多增加1.4%(49.7%的对比是Jorge Sangre的48.3%),我的男人Jorge Sangre通常也能胜出。
  读取固定大小的块中的数据通常会将一条线分成两个块。由于第一行末尾没有换行符,因此不计入该行的末尾。第二块中的片段确实会产生投票,但是它与任何候选人的姓名都不匹配,因此“ candidate”变量不会得到更新。这样的效果是将投票从名称被分割的候选人转移到获得先前投票的候选人。一个较长的名字更有可能被分割成多个区域,因此,阿尔伯托·阿布斯托(Alberto Arbusto)最终比乔尔·桑格里(Jorge Sangre)成为投票“捐助者”。