【问题标题】:CS50 pset3 Check50 is working but when I test my code there is something wrongCS50 pset3 Check50 正在工作,但是当我测试我的代码时出现问题
【发布时间】:2021-08-28 15:53:50
【问题描述】:

编写此代码时,我一直使用check50 检查我的程序,这些是我的结果:

:) runoff.c exists
:) runoff compiles
:) vote returns true when given name of candidate
:) vote returns false when given name of invalid candidate
:) vote correctly sets first preference for first voter
:) vote correctly sets third preference for second voter
:) vote correctly sets all preferences for voter
:) tabulate counts votes when all candidates remain in election
:) tabulate counts votes when one candidate is eliminated
:) tabulate counts votes when multiple candidates are eliminated
:) tabulate handles multiple rounds of preferences
:) print_winner prints name when someone has a majority
:) print_winner returns true when someone has a majority
:) print_winner returns false when nobody has a majority
:) print_winner returns false when leader has exactly 50% of vote
:) find_min returns minimum number of votes for candidate
:) find_min returns minimum when all candidates are tied
:) find_min ignores eliminated candidates
:) is_tie returns true when election is tied
:) is_tie returns false when election is not tied
:) is_tie returns false when only some of the candidates are tied
:) is_tie detects tie after some candidates have been eliminated
:) eliminate eliminates candidate in last place
:) eliminate eliminates multiple candidates in tie for last
:) eliminate eliminates candidates after some already eliminated

它似乎有效,但是当我使用 cs50 示例对其进行测试时,输出结果有所不同。 我该怎么办?

#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
int preferences[MAX_VOTERS][MAX_CANDIDATES];

// Candidates have name, vote count, eliminated status
typedef struct
{
    string name;
    int votes;
    bool eliminated;
}
candidate;

// Array of candidates
candidate candidates[MAX_CANDIDATES];

// Numbers of voters and candidates
int voter_count;
int candidate_count;

// Function prototypes
bool vote(int voter, int rank, string name);
void tabulate(void);
bool print_winner(void);
int find_min(void);
bool is_tie(int min);
void eliminate(int min);

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

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX_CANDIDATES)
    {
        printf("Maximum number of candidates is %i\n", MAX_CANDIDATES);
        return 2;
    }
    for (int i = 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);
        return 3;
    }

    // Keep querying for votes
    for (int i = 0; i < voter_count; i++)
    {

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            // Record vote, unless it's invalid
            if (!vote(i, j, name))
            {
                printf("Invalid vote.\n");
                return 4;
            }
        }

        printf("\n");
    }

    // Keep holding runoffs until winner exists
    while (true)
    {
        // Calculate votes given remaining candidates
        tabulate();

        // Check if election has been won
        bool won = print_winner();
        if (won)
        {
            break;
        }

        // Eliminate last-place candidates
        int min = find_min();
        bool tie = is_tie(min);

        // If tie, everyone wins
        if (tie)
        {
            for (int i = 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 (int i = 0; i < candidate_count; i++)
        {
            candidates[i].votes = 0;
        }
    }
    return 0;
}

// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (strcmp(candidates[i].name, name) == 0)
        {
            preferences[voter][rank] = i;
            return true;
        }
        
     }
    
    return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    for (int i = 0; i < voter_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (! candidates[preferences[i][j]].eliminated)
            {
                candidates[preferences[i][j]].votes++;
                break;
            }
        }
    }
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].votes > voter_count / 2)
        {
            printf("%s\n", candidates[i].name);
            return true;
        }
    }
    
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    int min = 0;
    int x;
    int y;
    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].eliminated;
                candidates[i].eliminated = candidates[j].eliminated;
                candidates[j].eliminated = x;
                
                y = candidates[i].votes;
                candidates[i].votes = candidates[j].votes;
                candidates[j].votes = y;
            }
        }
    }
    
   for (int k = 0; k < candidate_count; k++)
   {
       if (!candidates[k].eliminated)
       {
           min = candidates[k].votes;
           break;
       }
   }
   
   return min;
}


// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    int ties = 0;
    int elim = 0;
    for (int i = 0; i < candidate_count; i++)
    {
        if (!candidates[i].eliminated)
        {
            if (candidates[i].votes == min)
            {
                ties++;
            }
        }
    }
    
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].eliminated)
        {
            elim++;
        }
    }
    
    if (ties == candidate_count - elim)
    {
        return true;
    }
   
    return false;
}

// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (!candidates[i].eliminated)
        {
            if (candidates[i].votes == min)
            {
                candidates[i].eliminated = true;
            }
        }
    }
    
    return;
}

例如通过使用这些输入:

./runoff Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Bob
Rank 3: Charlie

Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob

Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice

Rank 1: Bob
Rank 2: Alice
Rank 3: Charlie

Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob

程序应该返回Alice,但它却返回Bob,我的代码有什么问题?

【问题讨论】:

  • 我花了一些时间分析代码,我对 tabulate() 有点怀疑。看起来每张选票都一样,即候选人的顺序(个人排名)无关紧要。那么这将是平局,因为每个候选人有 5 票。
  • 在我看来,您应该删除 tabulate() 中的 j 循环并将 j 更改为 0(例如:使用 j = 0 对循环进行一次迭代)
  • 抱歉,在 if oops 中没有看到中断
  • 别担心,谢谢。

标签: c cs50


【解决方案1】:

find_min 中的位交换候选者不交换名称。

这是个问题:

使用您的示例输入,在第一个径流候选人 [] 中的 find_min() 之前处于以下状态:Alice,2;Bob,2;Charlie,1 (此时也到处都是错误的,我在这里省略了它) find_min() 将其更改为Alice,1;Bob2;Charlie,2 – 看看这里有什么问题?查理犯下选举舞弊并进入下一次决选。

程序继续消除 Alice 并再次计票,得到 Bob 的 3 票和 Charlie 的 2 票(它的工作方式与预期的差不多)。

排序时交换时出现该错误。

编辑:

当您将候选人[i] 与候选人[j] 交换时(顺便说一句,您可以在 1 步中使用 tmp 候选人而不是 3 步),这会与选民的偏好相混淆。

例如:第一个选民正在对 A、B、C 进行投票,因此他的偏好是 nr0,nr1,nr2(使用来自候选人 [] 的索引)。但是,在您交换 A 和 C/nr 0 和 nr 2 之后(如我原来的答案中所述),第一个选民的偏好没有改变,仍然是:nr0, nr1,nr2,但现在 nr0 是查理,而不是安妮.所以这就像第一个选民投票C,B,A 而不是A,B,C

查理不算数,因为他现在被淘汰了,所以 nr1 (Bob) 获得了第一个选民的投票。两个好的解决方案是在交换期间相应地改变偏好,或者根本不交换(我更喜欢)。

第一个解决方案:

int find_min(void)
{
    int min = 0;
    candidate tmp;
    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 candidates
                tmp = candidates[i];
                candidates[i] = candidates[j];
                candidates[j] = tmp;
                
                //adjusting preferences
                for(int a = 0; a < voter_count; a++) {
                    for(int b = 0; b < candidate_count; b++) {
                        if(preferences[a][b] == i) {
                            prefereences[a][b] = j;
                        }
                        else if (preferences[a][b] == j) {
                            prefereences[a][b] = i;
                        }
                    }
                }
                
            }
        }
    }
    
   for (int k = 0; k < candidate_count; k++)
   {
       if (!candidates[k].eliminated)
       {
           min = candidates[k].votes;
           break;
       }
   }
   
   return min;
}

第二种解决方案(我认为更优雅,更快):

int find_min(void)
{
    int min = voter_count;//the max poss value
    for (int i = 0; i < candidate_count - 1; i++) 
    {
        if (!candidates[i].eliminated) {
            if (candidates[i].votes < min) {
                min = candidates[i].votes;
            }
        }
    }
    return min;
}

【讨论】:

  • 看来你是对的,我实现了它 (temp1 = candidates[i].name; candidates[i].name = candidates[j].name; candidates[j].name = temp1;) 但它仍然不适用于这些输入。
  • 编辑:要更改的第二件事已添加到答案中。
  • 想到的其他解决方案变化很大。另外:我希望这最终能奏效!
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 2022-08-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多