投票問題

輸入若干候選人,以及投票,格式如下,輸出(按輸入候選人輸入順序)候選人以及得票,以及
無效票數。
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

用鏈表做的,查找不高效,因爲要保持輸入順序。

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstdlib>  
  4. #include <string>  
  5. #include <cstring>  
  6. #include <algorithm>  
  7. #include <vector>  
  8. #include <sstream>  
  9. #include <set>  
  10. #include <map>  
  11. #include <cmath>  
  12.   
  13. using namespace std;  
  14. typedef struct person{  
  15.     string name;  
  16.     int num;  
  17.     person *next;  
  18.     person(string name, int num)  
  19.     {  
  20.         this->name = name;  
  21.         this->num = num;  
  22.         this->next = NULL;  
  23.     }  
  24. }person;  
  25.   
  26. void destroyList(person *head)  
  27. {  
  28.     //delete the person node's memory here.  
  29.     return;  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     string lines;  
  35.     string word;  
  36.     string name;  
  37.     person *head = NULL;  
  38.     person *tail = NULL;  
  39.     while(getline(cin, lines))  
  40.     {  
  41.         istringstream iss(lines);  
  42.         iss>>word;  
  43.         iss>>name;  
  44.         if (word != "add")  
  45.         {  
  46.             break;  
  47.         }  
  48.           
  49.         person * cur = new person(name,0);  
  50.         if (head == NULL)  
  51.         {  
  52.             head = cur;  
  53.             tail = cur;  
  54.         }  
  55.         else   
  56.         {  
  57.             tail->next = cur;  
  58.             tail = cur;  
  59.         }  
  60.     }  
  61.   
  62.     int illegal = 0;  
  63.   
  64.     person *tmp = head;  
  65.     while(tmp != NULL)  
  66.     {  
  67.         if (tmp->name == name)  
  68.         {  
  69.             tmp->num++;  
  70.             break;  
  71.         }  
  72.         else tmp = tmp->next;  
  73.     }  
  74.     while(getline(cin, lines))  
  75.     {  
  76.         istringstream iss(lines);  
  77.         iss>>word;  
  78.         iss>>name;  
  79.         if (word == "vote")  
  80.         {  
  81.             tmp = head;  
  82.             while(tmp != NULL)  
  83.             {  
  84.                 if (tmp->name == name)  
  85.                 {  
  86.                     tmp->num++;  
  87.                     break;  
  88.                 }  
  89.                 else tmp = tmp->next;  
  90.             }  
  91.             if (tmp == NULL)  
  92.             {  
  93.                 illegal++;  
  94.             }  
  95.         }  
  96.     }  
  97.   
  98.     tmp = head;  
  99.     while(tmp != NULL)  
  100.     {  
  101.         if (tmp->num > 0)  
  102.         {  
  103.             cout<<tmp->name<<" "<<tmp->num<<endl;  
  104.         }  
  105.         tmp = tmp->next;  
  106.     }  
  107.     cout<<illegal<<endl;  
  108.     destroyList(head);  
  109.   
  110.     system("pause");  
  111.     return 0;  
  112. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章