CS50 Pset3 Tideman-锁对和打印赢家

时间:2020-09-30 15:19:01

标签: cs50

我一直在Tideman pset3的lock_pair和print_winner上遇到问题(上下文在这里:https://cs50.harvard.edu/x/2020/psets/3/tideman/#:~:text=check50%20cs50/problems/2020/x/tideman)。尽力调试,但似乎无法跳过创建循环的配对。请寻求所有帮助。

:) tideman.c存在

:) tideman编译

:)当给定候选人姓名时,投票返回true

:)当给定无效候选人的姓名时,投票将返回false

:)投票正确设置了优先级排名

:)投票正确设置了所有首选项的排名

:) record_preferences正确设置了第一选民的首选项

:) record_preferences正确设置了所有选民的偏好

:) add_pairs在没有联系时会生成正确的对数

:) add_pairs在存在联系时生成正确的对数

:) add_pairs用获胜对填充对数组

:) add_pairs不会用丢失的对填充对数组

:) sort_pairs按获胜率对候选人对进行排序

:) lock_pairs在没有周期时锁定所有对

:( lock_pairs如果创建循环则跳过最后一对

lock_pairs did not correctly lock all non-cyclical pairs

:( lock_pairs如果创建周期则跳过中间对

lock_pairs did not correctly lock all non-cyclical pairs

:( print_winner在一个候选人胜过所有其他候选人时打印出选举的获胜者

print_winner did not print winner of election

:((print_winner会在某些对并列时显示选举的获胜者

print_winner did not print winner of election
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [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] = argv[i + 1];
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

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

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

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

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    for(int r = 0; r < candidate_count; r++)
    {
        for(int rc = 0; rc < candidate_count; rc++)
        {
            if(r < rc)
            {
                preferences[ranks[r]][ranks[rc]]++;
            }
        }
    }
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    for(int row = 0; row < candidate_count; row++)
    {
        for(int column = 0; column < candidate_count; column++)
        {
            if(preferences[row][column] > preferences[column][row])
            {
                pairs[pair_count].winner = row;
                pairs[pair_count].loser = column;
                pair_count++;
            }
        }
    }
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    int maxindex;
    for(int a = 0; a < pair_count - 1; a++)
    {
        maxindex = a;
        int largestdiff = preferences[pairs[a].winner][pairs[a].loser] - preferences[pairs[a].loser][pairs[a].winner];

        for(int b = a + 1; b < pair_count; b++)
        {
            if(preferences[pairs[b].winner][pairs[b].loser] - preferences[pairs[b].loser][pairs[b].winner] > largestdiff)
            {
                largestdiff = preferences[pairs[b].winner][pairs[b].loser] - preferences[pairs[b].loser][pairs[b].winner];
                maxindex = b;
            }
        }
        pair max = pairs[maxindex];
        pairs[maxindex] = pairs[a];
        pairs[a] = max;
    }
}

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    //Set everything to false first
    for(int row = 0; row < candidate_count; row++)
    {
        for(int column = 0; column < candidate_count; column++)
        {
            locked[row][column] = false;
        }
    }

    //Set the first pair to true
    locked[pairs[0].winner][pairs[0].loser] = true;

    //Iterate each pair and skip if there is a cycle
    for(int b = 1; b < pair_count; b++)
    {
        for(int c = b - 1; c > -1; c--)
        {
            if(pairs[b].winner == pairs[c].loser)
            {
               locked[pairs[b].winner][pairs[b].loser] = false;
               break;
            }
        }
        locked[pairs[b].winner][pairs[b].loser] = true;
    }
}

// Print the winner of the election
void print_winner(void)
{
    string winner = candidates[0];
    for(int column = 0; column < candidate_count; column++)
    {
        for(int row = 0; row < candidate_count; row++)
        {
            if(locked[row][column] == true)
            {
                winner = candidates[column + 1];
                break;
            }
            else
            {
                winner = candidates[column];
                if(row == candidate_count)
                {
                    break;
                }
            }
        }
    }
    printf("%s\n", winner);
}

1 个答案:

答案 0 :(得分:0)

首先,您不需要在lock_pairs函数中将所有锁定对设置为false。主要功能已经做到了。干燥。 其次,您没有正确检查周期。给定一对A击败B,您应该遍历所有输给B的候选人。然后,您应该遍历所有输给B的候选人的所有候选人。然后遍历那些输给那些输给B的候选人的候选人。输给B的人,依此类推。如果在任何时候弹出A,您将立即结束循环。这是一个循环。 A对B拍不应该被锁定。否则,如果迭代在没有找到A的情况下结束,则不是一个循环,您可以锁定该对。