Ultraviolet A Problem 10142 export Alian Voting (Australian vote)

Source: Internet
Author: User
// Export Alian Voting (Australian Voting) // PC/Ultraviolet IDs: 110108/10142, Popularity: B, Success rate: low Level: 1 // Verdict: Accepted // Submission Date: 2011-05-22 // UV Run Time: 0.844 s // copyright (C) 2011, Qiu. Metaphysis # yeah dot net # include <iostream> # include <sstream> # include <vector> # include <iterator> using namespace std; void voting (int current, int ncandidates, vector <string> & candidate, vector <int> & vote, vector <int> & choice) {// count the number of votes of the preferred candidate. For (int c = 0; c <choice. size (); c ++) vote [choice [c] [0]-1] ++; int nchoices = choice. size (); // total number of votes. While (1) {bool tied = true; // whether the number of candidates is the same. Int min = 1000; // used to find candidates with the least votes, up to 1000 votes. Int first =-1; for (int c = 0; c <ncandidates; c ++) {// if someone has more than half of the votes, it is determined that this person has won the election, otherwise, continue counting the votes. If (vote [c]> (nchoices/2) {cout <candidate [c] <endl; return ;}// gets the number of votes of the first candidate who is not out. If (vote [c]> = 0 & first =-1) first = vote [c]; // if the number of votes of other candidates is different from the number of unclaimed candidates currently, it's not a draw. If (first> = 0 & vote [c]> = 0 & vote [c]! = First) tied = false; // find the candidate with the least votes. If (vote [c]> = 0 & vote [c] <min) min = vote [c];} // check whether all candidates have the same number of votes. If (tied) {for (int I = 0; I <ncandidates; I ++) if (vote [I]> = 0) cout <candidate [I] <endl; return;} // remove the candidates with the least votes and their positions in the votes based on the min value. For (int I = 0; I <choice. size (); I ++) {// do you need to recalculate the vote. Bool recount = vote [choice [I] [0]-1] = min; for (int j = choice [I]. size ()-1; j> = 0; j --) if (vote [choice [I] [j]-1] = min) choice [I]. erase (choice [I]. begin () + j); if (recount) vote [choice [I] [0]-1] + +;} // The number of votes selected is-1, indicating that the ticket is out. For (int n = 0; n <ncandidates; n ++) if (vote [n] = min) vote [n] =-1 ;}} int main (int ac, char * av []) {vector <string> candidate; // candidate. Vector <int> vote; // Number of votes the candidate receives. Vector <int> choice; // records votes. Int cases; // Number of test data groups. Int current = 0; // number of data groups currently processed. Int ncandidates; // The total number of data candidates in each group. String line; // used to read data. // Read the number of test data groups. Cin> cases; // read the data of each group. While (current <cases) {cin> ncandidates; // whether the number of candidates is greater than 0. If (ncandidates> 0) {cin. ignore (); candidate. clear (); candidate. resize (ncandidates); // read the candidate list. For (int I = 0; I <ncandidates; I ++) {getline (cin, line); candidate [I] = line;} // read votes. Choice. clear (); while (getline (cin, line), line. length ()> 0) {istringstream iss (line); vector <int> tmp; tmp. resize (ncandidates); for (int I = 0; I <ncandidates; I ++) iss> tmp [I]; choice. push_back (tmp);} // the number of votes of the initial candidate is 0. Vote. clear (); vote. resize (ncandidates); fill (vote. begin (), vote. end (), 0); // simulate the counting process of votes. Voting (current, ncandidates, candidate, vote, choice);} current ++; // process input empty rows. If (current <cases & ncandidates> 0) cout <'\ n';} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.