首页 > 解决方案 > CS50 复数,在平局的情况下不打印多个获胜者

问题描述

我使用 check50 检查了我的代码,除了一个之外,所有条件都满足print_winner(),如果出现平局,该功能不会打印多个获胜者,我不知道为什么它不起作用。

这些是我的结果:

:) plurality.c exists
:) plurality compiles
:) vote returns true when given name of first candidate
:) vote returns true when given name of middle candidate
:) vote returns true when given name of last candidate
:) vote returns false when given name of invalid candidate
:) vote produces correct counts when all votes are zero
:) vote produces correct counts after some have already voted
:) vote leaves vote counts unchanged when voting for invalid candidate
:) print_winner identifies Alice as winner of election
:) print_winner identifies Bob as winner of election
:) print_winner identifies Charlie as winner of election
:( print_winner prints multiple winners in case of tie
    print_winner function did not print both winners of election
:) print_winner prints all names when all candidates are tied

我应该在以下代码中修复什么?

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct {
    string name;
    int votes;
} candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

int voter_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[]) {
    // Check for invalid usage
    if (argc < 2) {
        printf("Usage: plurality [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX) {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++) {
        candidates[i].name = argv[i + 1];
        candidates[i].votes = 0;
    }

    voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++) {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name)) {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string name) {
    for (int i = 0; i < candidate_count; i++) {
        if (strcmp(candidates[i].name, name) == 0) {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
string x;

void print_winner(void) {
    for (int i = 0; i < candidate_count - 1; i++) {
        for (int j = i + 1; j < candidate_count; j++) {
            if (candidates[j].votes < candidates[i].votes) {
                // swapping elements
                x = candidates[i].name;
                candidates[i].name = candidates[j].name;
                candidates[j].name = x;
            }
        }
    }
    
    for (int k = 0; k < candidate_count; k++) {
        if (candidates[k].votes == candidates[candidate_count - 1].votes) {
            printf("%s\n", candidates[k].name);
        }
    }
    return;
}

标签: ccs50

解决方案


您的排序代码仅对候选人姓名进行排序,而不是对候选人选票进行排序。请注意,无需按票数对候选人进行排序,您只需确定最大票数并打印具有该票数的所有候选人即可。print_winners()为保持一致性,应重命名该函数。还要删除全局变量,无论如何x它应该是本地的。print_winner()

这是修改后的版本:

void print_winners(void) {
    int max_votes = candidates[0].votes;
    for (int i = 1; i < candidate_count; i++) {
        if (max_votes < candidates[i].votes) {
            max_votes = candidates[i].votes;
        }
    }
    for (int i = 0; i < candidate_count; i++) {
        if (candidates[i].votes == max_votes) {
            printf("%s\n", candidates[i].name);
        }
    }
}

推荐阅读