1389.cpp

来自「哈尔滨工业大学ACM 竞赛网上在线试题集锦的源代码」· C++ 代码 · 共 73 行

CPP
73
字号
/*  This Code is Submitted by wywcgs for Problem 1389 on 2005-03-12 at 09:30:56 */ 
#include <stdio.h>
#include <string.h>

typedef struct{
    int Ballot;
    char party[81];
    char name[81];
    char *fParty;
}Candidater;

int main()
{
    Candidater candidater[20];
    int numCand, numBallot, i, j, flag = 0, inttemp;
    char temp[81], *ftemp;

    while(scanf("%d", &numCand) == 1){
        while(getchar() != '\n')
            ;
        for(i = 0; i < 20; i++){
            candidater[i].Ballot = 0;
            for(j = 0; j < 81; j++){
                candidater[i].party[j] = 0;
                candidater[i].name[j] = 0;
            }
        }
        for(i = 0; i < numCand; i++){
            gets(candidater[i].name);
            gets(candidater[i].party);
            candidater[i].fParty = candidater[i].party;
        }
        scanf("%d", &numBallot);
        while(getchar() != '\n')
            ;
        for(i = 0; i < numBallot; i++){
            for(j = 0; j < 81; j++){
                temp[j] = 0;
            }
            gets(temp);
            for(j = 0; j < numCand; j++){
                if(!strcmp(temp, candidater[j].name)){
                    candidater[j].Ballot++;    
                    break;
                }
            }
        }
        for(i = 0; i < numCand; i++){
            flag = 0;
            for(j = 0; j < numCand-1; j++){
                if(candidater[j].Ballot < candidater[j+1].Ballot){
                    inttemp = candidater[j].Ballot;
                    candidater[j].Ballot = candidater[j+1].Ballot;
                    candidater[j+1].Ballot = inttemp;
                    ftemp = candidater[j].fParty;
                    candidater[j].fParty = candidater[j+1].fParty;
                    candidater[j+1].fParty = ftemp;
                    flag = 1;
                }
            }
            if(flag == 0){
                break;
            }
        }
        if(candidater[0].Ballot == candidater[1].Ballot){
            printf("tie\n");
        }else{
            printf("%s\n", candidater[0].fParty);
        }
    }

    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?