【华为编程大赛】投票问题

输入若干候选人,以及投票,格式如下,输出(按输入候选人输入顺序)候选人以及得票,以及
无效票数。
Input:
addCandidate xx1
addCandidate xx2
addCandidate xx3
addCandidate xx4
addCandidate xx5
addCandidate xx6
vote xx2
vote xx2
vote xx3
vote xx3
vote xx4
vote xx6
vote xx7
vote xx1
vote xx1
Output:
xx1 2
xx2 2
xx3 2
xx4 1
xx6 1

1

用链表做的,查找不高效,因为要保持输入顺序。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>
#include <map>
#include <cmath>

using namespace std;
typedef struct person{
	string name;
	int num;
	person *next;
	person(string name, int num)
	{
		this->name = name;
		this->num = num;
		this->next = NULL;
	}
}person;

void destroyList(person *head)
{
	//delete the person node's memory here.
	return;
}

int main()
{
	string lines;
	string word;
	string name;
	person *head = NULL;
	person *tail = NULL;
	while(getline(cin, lines))
	{
		istringstream iss(lines);
		iss>>word;
		iss>>name;
		if (word != "add")
		{
			break;
		}
		
		person * cur = new person(name,0);
		if (head == NULL)
		{
			head = cur;
			tail = cur;
		}
		else 
		{
			tail->next = cur;
			tail = cur;
		}
	}

	int illegal = 0;

	person *tmp = head;
	while(tmp != NULL)
	{
		if (tmp->name == name)
		{
			tmp->num++;
			break;
		}
		else tmp = tmp->next;
	}
	while(getline(cin, lines))
	{
		istringstream iss(lines);
		iss>>word;
		iss>>name;
		if (word == "vote")
		{
			tmp = head;
			while(tmp != NULL)
			{
				if (tmp->name == name)
				{
					tmp->num++;
					break;
				}
				else tmp = tmp->next;
			}
			if (tmp == NULL)
			{
				illegal++;
			}
		}
	}

	tmp = head;
	while(tmp != NULL)
	{
		if (tmp->num > 0)
		{
			cout<<tmp->name<<" "<<tmp->num<<endl;
		}
		tmp = tmp->next;
	}
	cout<<illegal<<endl;
	destroyList(head);

	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
华为编程规范 PDF 是一份华为公司针对软件开发编写的规范文档,旨在规范开发人员在编写代码时的风格和规范。这份文档通过提供统一的编程规范,帮助开发人员编写更加规范、易读、易维护的代码,并提高软件开发的质量和效率。 华为编程规范 PDF 包含了丰富的编程规范内容,涵盖了代码风格、命名规范、注释规范、代码组织等方面。通过按照规范进行开发,可以提高代码的可读性和可维护性,方便其他开发人员理解和修改代码,并减少潜在的Bug风险。 编程规范的内容丰富全面,例如在命名规范方面,规定了变量、函数、类的命名规则,使得命名更加清晰易懂;在代码风格方面,规范了缩进、空格、注释等细节,使得代码的格式化更加整齐一致;在代码组织方面,规定了代码的模块化、文件结构的组织方式,方便代码管理和维护。 华为编程规范 PDF 不仅对华为公司内部的开发人员有指导作用,同时也可以作为一份优秀的编程规范参考文档,供其他软件开发团队借鉴和参考。在保证代码风格一致性的同时,还可以减少团队内部的代码冲突和重复工作,提高开发效率。 总之,华为编程规范 PDF 是一份帮助开发人员编写规范、易读、易维护代码的重要文档,能够提高软件开发质量和效率,对于开发团队和个人来说都具有重要的指导作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值