#include<cs50.h>#include<stdio.h>#include<string.h>// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedefstruct{stringname;intvotes;}candidate;// declare functions
intselectionSort(candidatepeople[]);intbubbleSort(candidatepeople[]);intmergeSort(candidatepeople[],intlength);// Array of candidates
candidatecandidates[MAX];// Number of candidates
intcandidate_count;// Function prototypes
boolvote(stringname);voidprint_winner(void);intmain(intargc,stringargv[]){// Check for invalid usage
if(argc<2){printf("Usage: plurality [candidate ...]\n");return1;}// Populate array of candidates
candidate_count=argc-1;if(candidate_count>MAX){printf("Maximum number of candidates is %i\n",MAX);return2;}for(inti=0;i<candidate_count;i++){candidates[i].name=argv[i+1];candidates[i].votes=0;}intvoter_count=get_int("Number of voters: ");// Loop over all voters
for(inti=0;i<voter_count;i++){stringname=get_string("Vote: ");// Check for invalid vote
if(!vote(name)){printf("Invalid vote.\n");i--;}}// Display winner of election
print_winner();}// Update vote totals given a new vote
boolvote(stringname){for(inti=0;i<candidate_count;i++){if(strcmp(name,candidates[i].name)==0){candidates[i].votes++;returntrue;}}returnfalse;}// Print the winner (or winners) of the election
voidprint_winner(void){// int highestVotes = selectionSort(candidates);
// int highestVotes = bubbleSort(candidates);
inthighestVotes=mergeSort(candidates,candidate_count);for(inti=0;i<candidate_count;i++){if(candidates[i].votes==highestVotes){printf("%s\n",candidates[i].name);}}return;}intselectionSort(candidatepeople[]){candidatetemp;for(inti=0;i<candidate_count;i++){for(intj=i+1;j<candidate_count;j++){if(people[i].votes>people[j].votes){temp=people[i];people[i]=people[j];people[j]=temp;}}}returnpeople[candidate_count-1].votes;}intbubbleSort(candidatepeople[]){candidatetemp;for(inti=0;i<candidate_count;i++){for(intj=0;j<candidate_count-i-1;j++){if(people[j].votes>people[j+1].votes){temp=people[j];people[j]=people[j+1];people[j+1]=temp;}}}returnpeople[candidate_count-1].votes;}intmergeSort(candidatepeople[],intlength){inthalf=length/2;if(half<1){return0;}candidateleftHalf[half];candidaterightHalf[length-half];for(inti=0;i<half;i++){leftHalf[i]=people[i];}for(inti=0;i<length-half;i++){rightHalf[i]=people[half+i];}mergeSort(leftHalf,half);mergeSort(rightHalf,length-half);intleftIndex=0;intrightIndex=0;for(inti=0;i<length;i++){if(leftIndex<half){if(leftHalf[leftIndex].votes<=rightHalf[rightIndex].votes){people[i]=leftHalf[leftIndex];leftIndex++;}else{people[i]=rightHalf[rightIndex];rightIndex++;}}else{people[i]=rightHalf[rightIndex];rightIndex++;}}returnpeople[length-1].votes;}
#include<cs50.h>#include<stdio.h>#include<string.h>// Max voters and candidates
#define MAX_VOTERS 100
#define MAX_CANDIDATES 9
// preferences[i][j] is jth preference for voter i
intpreferences[MAX_VOTERS][MAX_CANDIDATES];// Candidates have name, vote count, eliminated status
typedefstruct{stringname;intvotes;booleliminated;}candidate;// Array of candidates
candidatecandidates[MAX_CANDIDATES];candidatetemp[MAX_CANDIDATES];// Numbers of voters and candidates
intvoter_count;intcandidate_count;boolexchange=true;stringwinner;// Function prototypes
boolvote(intvoter,intrank,stringname);voidtabulate(void);boolprint_winner(void);intfind_min(void);boolis_tie(intmin);voideliminate(intmin);boolselectionSort(void);boolbubbleSort(void);boolmergeSort(void);voidmergeSorting(candidatetemp[MAX_CANDIDATES],intcount);intmain(intargc,stringargv[]){// Check for invalid usage
if(argc<2){printf("Usage: runoff [candidate ...]\n");return1;}// Populate array of candidates
candidate_count=argc-1;if(candidate_count>MAX_CANDIDATES){printf("Maximum number of candidates is %i\n",MAX_CANDIDATES);return2;}for(inti=0;i<candidate_count;i++){candidates[i].name=argv[i+1];candidates[i].votes=0;candidates[i].eliminated=false;}voter_count=get_int("Number of voters: ");if(voter_count>MAX_VOTERS){printf("Maximum number of voters is %i\n",MAX_VOTERS);return3;}// Keep querying for votes
for(inti=0;i<voter_count;i++){// Query for each rank
for(intj=0;j<candidate_count;j++){stringname=get_string("Rank %i: ",j+1);// Record vote, unless it's invalid
if(!vote(i,j,name)){printf("Invalid vote.\n");return4;}}printf("\n");}// Keep holding runoffs until winner exists
while(true){// Calculate votes given remaining candidates
tabulate();// Check if election has been won
boolwon=print_winner();if(won){break;}// Eliminate last-place candidates
intmin=find_min();booltie=is_tie(min);// If tie, everyone wins
if(tie){for(inti=0;i<candidate_count;i++){if(!candidates[i].eliminated){printf("%s\n",candidates[i].name);}}break;}// Eliminate anyone with minimum number of votes
eliminate(min);// Reset vote counts back to zero
for(inti=0;i<candidate_count;i++){candidates[i].votes=0;}}return0;}// Record preference if vote is valid
boolvote(intvoter,intrank,stringname){for(inti=0;i<candidate_count;i++){if(strcmp(name,candidates[i].name)==0){preferences[voter][rank]=i;returntrue;}}returnfalse;}// Tabulate votes for non-eliminated candidates
voidtabulate(void){for(inti=0;i<voter_count;i++){for(intj=0;j<candidate_count;j++){intindex=preferences[i][j];if(candidates[index].eliminated==true){continue;}else{candidates[index].votes++;break;}}}return;}// Print the winner of the election, if there is one
boolprint_winner(void){if(mergeSort()){printf("%s\n",winner);returntrue;}returnfalse;}// Return the minimum number of votes any remaining candidate has
intfind_min(void){intmin=voter_count+1;for(inti=0;i<candidate_count;i++){if(candidates[i].eliminated!=true){if(candidates[i].votes<min){min=candidates[i].votes;}}}returnmin;}// Return true if the election is tied between all candidates, false otherwise
boolis_tie(intmin){for(inti=0;i<candidate_count;i++){if(candidates[i].eliminated!=true){if(candidates[i].votes==min){continue;}else{returnfalse;}}}returntrue;}// Eliminate the candidate (or candidates) in last place
voideliminate(intmin){for(inti=0;i<candidate_count;i++){if(candidates[i].votes==min){candidates[i].eliminated=true;}if(temp[i].votes==min){temp[i].eliminated=true;}}return;}boolselectionSort(void){for(inti=0;i<candidate_count;i++){temp[i]=candidates[i];}for(inti=0;i<candidate_count-1;i++){for(intj=i+1;j<candidate_count;j++){if(temp[j].votes<temp[i].votes){candidatereplace=temp[i];temp[i]=temp[j];temp[j]=replace;}}}if(temp[candidate_count-1].votes>temp[candidate_count-2].votes&&temp[candidate_count-1].votes>(voter_count/2)){winner=temp[candidate_count-1].name;returntrue;}returnfalse;}boolbubbleSort(void){for(inti=0;i<candidate_count;i++){temp[i]=candidates[i];}for(inti=0;exchange&&i<candidate_count-1;i++){exchange=false;for(intj=0;j<candidate_count-1-i;j++){candidatereplace=temp[j];if(temp[j].votes>temp[j+1].votes){replace=temp[j];temp[j]=temp[j+1];temp[j+1]=replace;exchange=true;}}}if(temp[candidate_count-1].votes>temp[candidate_count-2].votes&&temp[candidate_count-1].votes>(voter_count/2)){winner=temp[candidate_count-1].name;returntrue;}returnfalse;}boolmergeSort(void){for(inti=0;i<candidate_count;i++){temp[i]=candidates[i];}mergeSorting(temp,candidate_count);if(temp[candidate_count-1].votes>temp[candidate_count-2].votes&&temp[candidate_count-1].votes>(voter_count/2)){winner=temp[candidate_count-1].name;returntrue;}returnfalse;}voidmergeSorting(candidatepeople[MAX_CANDIDATES],intcount){intleftPart=count/2;intrightPart=count-leftPart;candidateleftSide[leftPart];candidaterightSide[rightPart];for(inti=0;i<leftPart;i++){leftSide[i]=people[i];}for(inti=0;i<rightPart;i++){rightSide[i]=people[i+leftPart];}if(leftPart<1){return;}else{mergeSorting(leftSide,leftPart);mergeSorting(rightSide,rightPart);}intleftIndex=0;intrightIndex=0;for(inti=0;i<count;i++){if(i<leftPart){if(leftSide[leftIndex].votes>=rightSide[rightIndex].votes){people[i]=rightSide[rightIndex];rightIndex++;}else{people[i]=leftSide[leftIndex];leftIndex++;}}else{people[i]=rightSide[rightIndex];rightIndex++;}}}
#include<cs50.h>#include<stdio.h>#include<string.h>// Max number of candidates
#define MAX 9
// preferences[i][j] is number of voters who prefer i over j
intpreferences[MAX][MAX];// locked[i][j] means i is locked in over j
boollocked[MAX][MAX];// Each pair has a winner, loser
typedefstruct{intwinner;intloser;}pair;// Array of candidates
stringcandidates[MAX];pairpairs[MAX*(MAX-1)/2];intpair_count;intcandidate_count;boolloop=true;// Function prototypes
boolvote(intrank,stringname,intranks[]);voidrecord_preferences(intranks[]);voidadd_pairs(void);voidsort_pairs(void);voidlock_pairs(void);voidprint_winner(void);voidselectionSort(void);intmain(intargc,stringargv[]){// Check for invalid usage
if(argc<2){printf("Usage: tideman [candidate ...]\n");return1;}// Populate array of candidates
candidate_count=argc-1;if(candidate_count>MAX){printf("Maximum number of candidates is %i\n",MAX);return2;}for(inti=0;i<candidate_count;i++){candidates[i]=argv[i+1];}// Clear graph of locked in pairs
for(inti=0;i<candidate_count;i++){for(intj=0;j<candidate_count;j++){locked[i][j]=false;}}pair_count=0;intvoter_count=get_int("Number of voters: ");// Query for votes
for(inti=0;i<voter_count;i++){// ranks[i] is voter's ith preference
intranks[candidate_count];// Query for each rank
for(intj=0;j<candidate_count;j++){stringname=get_string("Rank %i: ",j+1);if(!vote(j,name,ranks)){printf("Invalid vote.\n");return3;}}record_preferences(ranks);printf("\n");}add_pairs();sort_pairs();lock_pairs();print_winner();return0;}// Update ranks given a new vote
// 把候选人的索引,根据其优先级,保存到rank数组里
boolvote(intrank,stringname,intranks[]){for(inti=0;i<candidate_count;i++){if(strcmp(name,candidates[i])==0){ranks[rank]=i;returntrue;}}returnfalse;}// Update preferences given one voter's ranks
voidrecord_preferences(intranks[]){for(inti=0;i<candidate_count-1;i++){for(intj=i+1;j<candidate_count;j++){preferences[ranks[i]][ranks[j]]++;}}return;}// Record pairs of candidates where one is preferred over the other
voidadd_pairs(void){intindex=0;for(inti=0;i<candidate_count-1;i++){for(intj=i+1;j<candidate_count;j++){if(preferences[i][j]>preferences[j][i]){pairs[index].winner=i;pairs[index].loser=j;index++;pair_count++;}elseif(preferences[i][j]<preferences[j][i]){pairs[index].winner=j;pairs[index].loser=i;index++;pair_count++;}else{continue;}}}return;}// Sort pairs in decreasing order by strength of victory
voidsort_pairs(void){selectionSort();return;}// Lock pairs into the candidate graph in order, without creating cycles
// 这里未通过测试
voidlock_pairs(void){for(inti=0;i<pair_count;i++){locked[pairs[i].winner][pairs[i].loser]=true;}for(inti=0;i<candidate_count-1;i++){for(intj=i+1;j<candidate_count;j++){if(locked[i][j]==true&&locked[j][i]==true){locked[i][j]=false;locked[j][i]=false;}}}for(inti=candidate_count-1;i<candidate_count;i++){for(intj=0;j<candidate_count;j++){if(locked[i][j]==true){for(intk=0;k<candidate_count-1;k++){intindex=k+j+1;if(index==candidate_count){index=0;}if(locked[k][index]==false){loop=false;}}}}}// int trueCount[pair_count];
// for (int i = 0; i < candidate_count; i++) {
// int tempTrueCount = 0;
// for(int j = 0; j <candidate_count; j++) {
// if(locked[i][j] == true) {
// tempTrueCount++;
// }
// }
// trueCount[i] = tempTrueCount;
// }
// for (int i = 0; i < pair_count - 1; i++) {
// if(trueCount[i] != trueCount[i+1]) {
// loop = false;
// break;
// }
// }
if(loop){for(inti=candidate_count-1;i<candidate_count;i++){for(intj=0;i<candidate_count;j++){locked[i][j]=false;}}}return;}// Print the winner of the election
voidprint_winner(void){if(loop){printf("%s\n",candidates[pairs[0].winner]);}else{inthighestVotesIndex;inthighestVotes=-1;for(inti=0;i<candidate_count;i++){inttempHighestVotes=0;for(intj=0;j<candidate_count;j++){if(locked[i][j]==true){tempHighestVotes++;}}if(tempHighestVotes>=highestVotes){highestVotesIndex=i;}}printf("%s\n",candidates[highestVotesIndex]);}return;}voidselectionSort(void){pairtemp;for(inti=0;i<pair_count-1;i++){for(intj=i+1;j<pair_count;j++){if(preferences[pairs[i].winner][pairs[i].loser]<preferences[pairs[j].winner][pairs[j].loser]){temp=pairs[i];pairs[i]=pairs[j];pairs[j]=temp;}}}}