#include int A[1000]; int main(int argc, char ** argv) { int i, N; int winner, votes; N = 1; while ( scanf("%d", &A[N]) > 0 ) N++; winner = A[1]; // assume A[1] is the winner... votes = 1; // A[1] has 1 "surplus" vote so far printf("Candidate = %d, surplus vote = %d\n", winner, votes); for ( i = 2; i <= N; i++ ) { if ( A[i] == winner ) { votes++; // One more vote for the assumed winner } else if ( votes > 0 ) { votes--; // One vote against the assumed winner... } else { // The assumed winner has no more surplus votes... winner = A[i]; // Pick a new assumed winner votes = 1; // A[i] has 1 "surplus" vote so far } printf("Candidate = %d, surplus vote = %d\n", winner, votes); } printf("Done...\n"); printf("Majority element = %d\n", winner); }