候选人投票统计

输入:  
输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。
 
输出:  
每行输出候选人的名字和得票数量。
 
样例输入:
4
A B C D
8
A B C D E F G H
                   
样例输出:
A : 1
B : 1
C : 1
D : 1

Invalid : 4


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
    char name[20];
    int voteCnt;
    struct Node *next;
}node;

node *createlist()
{
    node *strList=(node*)malloc(sizeof(node));
    strList->next=NULL;
    return strList;
}

int getlength(node *head)
{
    int cnt=0;
    node *p=head;
    while(p->next!=NULL)
    {
        cnt++;
        p=p->next;
    }
    return cnt;
}

void insertCandidate(node *head,char *name)
{
    node *p=head;
    if(p==NULL||name==NULL)
        return;
    while(p->next!=NULL)
    {
            p=p->next;
    }
    node *newNode=(node*)malloc(sizeof(node));
    strcpy(newNode->name,name);
    newNode->voteCnt=0;
    p->next=newNode;
    newNode->next=NULL;
}

void addVote(node *head,char *name)
{
    node *p=head->next;
    if(p==NULL||name==NULL)
        return;
    while(p!=NULL)
    {
        if(strcmp(p->name,name)==0)
        {
            p->voteCnt++;
            return;
        }
        p=p->next;
    }
}

int main()
{
    int cn,vn,i,validCnt;
    char name[20];
    node *candidateList=createlist();
    node *p;
    scanf("%d",&cn);
    for(i=0;i<cn;i++)
    {
        scanf("%s",name);
        insertCandidate(candidateList,name);
    }
    scanf("%d",&vn);
    for(i=0;i<vn;i++)
    {
        scanf("%s",name);
        addVote(candidateList,name);
    }
    validCnt=0;
    p=candidateList->next;
    while(p!=NULL)
    {
        printf("%s:%d\n",p->name,p->voteCnt);
        validCnt+=p->voteCnt;
        p=p->next;
    }
    printf("Invalid:%d",vn-validCnt);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值