简体   繁体   English

根据 CS50 的 pset3 tideman 中的 check50 排序对不起作用

[英]Sorting pairs is not working according to check50 in CS50's pset3 tideman

I am currently working on CS50's pset3 called Tideman.我目前正在研究名为 Tideman 的 CS50 的 pset3。 I am new to coding and I would love some input of someone who has some more experience.我是编码新手,我希望有更多经验的人提供一些意见。

One of the tasks for the student is to sort the pairs created during the tideman method of voting.学生的任务之一是对在潮人投票方法中创建的对进行排序。 My question continues below my current code, the part that matters is void sort_pairs(void) and I have added numbers to the left side so that it pops out.我的问题继续在我当前的代码下方,重要的部分是 void sort_pairs(void) 并且我在左侧添加了数字以使其弹出。

    #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
    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 i = 0; i < candidate_count; i++)
        {
            for (int j = i + 1; j < candidate_count; j++)
            {
                preferences[ranks[i]][ranks[j]]++;
            }
        }
        return;
    }

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

                else if (preferences[i][j] < preferences[j][i])
                {
                    pairs[pair_count].winner = j;
                    pairs[pair_count].loser = i;
                    pair_count++;
                }
            }
        }
        return;
    }

1     // Sort pairs in decreasing order by strength of victory
2     void sort_pairs(void)
3     {
4         // fill array with winner preferences
5         int onearr[100] = { 0 };
6         for (int i = 0; i < pair_count; i++)
7         {
8             onearr[i] = preferences[pairs[i].winner][pairs[i].loser];
9         }
10        //sorting function
11        while (1)
12        {
13            int swapped = 0;
14            for (int i = 0; i < pair_count - 1; i++)
15            {
16                if (onearr[i] < onearr[i + 1])
17                {
18                    int temp = onearr[i];
19                    onearr[i] = onearr[i + 1];
20                    onearr[i + 1] = temp;
21                    swapped = 1;
22                }
23            }
24            if (swapped == 0)
25            {
26                break;
27            }
28        }
29        return;
30    }

    // Lock pairs into the candidate graph in order, without creating cycles
    void lock_pairs(void)
    {
        // TODO
        return;
    }

    // Print the winner of the election
    void print_winner(void)
    {
        // TODO
        return;
    }

My question specifically is that the sorting function seems to work fine (I added printf statements all over the place to check if it correctly sorts, and it does) but check50 tells me sort_pairs does not correctly sort pairs.我的具体问题是排序 function 似乎工作正常(我在各处添加了 printf 语句以检查它是否正确排序,并且确实如此)但是 check50 告诉我 sort_pairs 没有正确排序对。 The last two lock_pairs(void) and print_winner(void) I have yet to define.最后两个 lock_pairs(void) 和 print_winner(void) 我还没有定义。

Thanks in advance提前致谢

Answer found:找到答案:

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // fill array with winner preferences
    int strength[pair_count];

    for (int i = 0; i < pair_count; i++)
    {
        strength[i] = preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser][pairs[i].winner];
    }

    //sorting function
    while (1)
    {
        int swapped = 0;

        for (int i = 0; i < pair_count - 1; i++)
        {
            for (int j = i + 1; j < pair_count; j++)
            {
                if (strength[i] < strength[j])
                {

                //sorting pairs
                pair tmp = pairs[i];
                pairs[i] = pairs[j];
                pairs[j] = tmp;

                //sorting strength
                int tmp2 = strength[j];
                strength[j] = strength[i];
                strength[i] = tmp2;
                }
            }
        }

        if (swapped == 0)
        {
            break;
        }
    }
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM