U E D R , A S I H C RSS

Australian Voting/Leonardong

No older revisions available

No older revisions available



미완
~cpp 
#include <iostream> 
#include <vector> 
using namespace std; 
 
#define IntVector vector<int> 
#define CandidatorVector vector<Candidator> 
#define VoteSheetVector vector<VoteSheet> 
struct Candidator 
{ 
        char name[80]; 
        int votedCount;
        bool fallen; 
}; 
 
struct VoteSheet 
{ 
   IntVector candidateNum; 
}; 
 
bool isWin( const Candidator & candidator, int n ) 
{ 
        if ( candidator.votedCount >= n / 2 ) 
                return true; 
        return false; 
} 
 
int current( const VoteSheet & sheet ) 
{ 
        return sheet.candidateNum.front(); 
} 
 
int pop_front( VoteSheet & sheet ) 
{ 
        return *sheet.candidateNum.erase( sheet.candidateNum.begin() ); 
} 
 
void collectVoting( CandidatorVector & candidators, const VoteSheetVector & sheets ) 
{ 
        for ( int i = 0 ; i < sheets.size() ; i++ ) 
                if ( candidators[ current(sheets[i]) ].fallen == false ) 
                        candidators[ current(sheets[i]) ].votedCount++; 
} 
 
void markFall( CandidatorVector & candidators, const int limit ) 
{ 
        for ( int i = 0 ; i < candidators.size() ; i++ ) 
                if ( candidators[i].votedCount <= limit ) 
                        candidators[i].fallen = false; 
} 
 
int minVotedNum( const CandidatorVector & candidators ) 
{ 
        int result = INT_MAX; 
        for ( int i = 0 ; i < candidators.size() ; i++ ) 
                if ( candidators[i].fallen == false) 
                        if ( result > candidators[i].votedCount ) 
                                result = candidators[i].votedCount; 
        return result; 
} 
 
bool isUnionWin( const CandidatorVector & candidators ) 
{ 
        for ( int i = 0 ; i < candidators.size() ; i++ ) 
                if ( candidators[i].fallen == false ) 
                        if ( candidators[i].votedCount != minVotedNum(candidators) ) 
                                return false; 
        return true; 
} 
 
int countRemainCandidators( const CandidatorVector & candidators ) 
{ 
        int result = 0; 
        for ( int i = 0 ; i < candidators.size() ; i++ ) 
                if ( candidators[i].fallen == false ) 
                        result++;
        return result; 
} 
bool isSomeoneWin( const CandidatorVector & candidators ) 
{ 
        int remainNum = countRemainCandidators(candidators); 
        for ( int i = 0 ; i < candidators.size() ; i++ ) 
                if ( isWin( candidators[i], remainNum ) )
                        return true; 
        return false; 
} 
void main() 
{ 
//        cin >> numOfCase; 
	int numOfCandidators;
    cin >> numOfCandidators; 

	char name[80];
	CandidatorVector candidators;

	int n = numOfCandidators;
	cin.get();
    for ( int i = 0 ; i < n ; i++ ){ 
		cin.getline( name, 80);
		Candidator candidator;
		strcpy( candidator.name, name );
		candidator.votedCount = 0;
		candidator.fallen = false;
		candidators.push_back( candidator );
	}
	int vote;
	VoteSheetVector sheets;
    for ( i = 0 ; i < 5 ; i++ ){
		VoteSheet sheet;
		for ( int j = 0 ; j < n ; j++ ) {
			cin >> vote; 
			sheet.candidateNum.push_back( vote );
		}
		sheets.push_back( sheet );
	}

	int num;
    do{
		for ( int i = 0 ; i < n ; i++ ) {
			num = pop_front(sheets[i]);
			candidators[ num ].votedCount++;
		}
		markFall( candidators, minVotedNum( candidators ) );
	}while( isUnionWin(candidators) && isSomeoneWin(candidators) );
	for ( i = 0 ; i < n ; i++ )
		if ( candidators[i].fallen == false )
			cout << candidators[i].name << endl;
} 
 


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:33
Processing time 0.0147 sec