【华为编程大赛】投票问题
生活随笔
收集整理的這篇文章主要介紹了
【华为编程大赛】投票问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入若干候選人,以及投票,格式如下,輸出(按輸入候選人輸入順序)候選人以及得票,以及
無效票數。
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
無效票數。
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; }總結
以上是生活随笔為你收集整理的【华为编程大赛】投票问题的全部內容,希望文章能夠幫你解決所遇到的問題。