【哈佛cs50 2022】lab3 & problemSet3【ing】

发布时间 2023-07-01 16:33:32作者: 致命一姬

(1)lab3

如何测试每个代码运行所需要的时间?time ./sort1 sorted5000.txt

  sort1 sort2 sort3
sorted5000.txt 0.037s 0.020s 0.045s
sorted10000.txt

0.058s

0.050s

0.151s

sorted50000.txt 1.244s 2.238s 5.637s
reversed5000.txt 0.088s 0.026s 0.045s
reversed10000.txt 0.279s 0.087s 0.142s
reversed50000.txt 7.326s 3.560s 5.474s
random5000.txt 0.096s 0.023s 0.071s
random10000.txt 0.287s

0.1s

0.143s

random50000.txt 8.405s 1.975s 4.808s
my answer  bubble sort, merge sort  selection sort

(2)problem set3 - plurality

#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;

// 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;
    }

    int 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)
{
    // TODO
    int compare = 0;
    for(int i=0;i<candidate_count;++i){
        compare = strcmp(candidates[i].name,name);
        if(!compare){
            candidates[i].votes+=1;
            //printf("compare result-%i,votes-%i\n",strcmp(candidates[i].name,name),candidates[i].votes);
            return true;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    // TODO
    int sort_max = 0;
    if(candidate_count == 1){
        printf("%s\n",candidates[candidate_count-1].name);
    }
    else{
        sort_max = candidates[0].votes;
        for(int i=0;i<candidate_count-1;++i){
            if(candidates[i+1].votes > candidates[i].votes){
                sort_max = candidates[i+1].votes;
            }
        }
        //printf("%i\n",sort_max);
        for(int j=0;j<candidate_count;++j){
            if(candidates[j].votes == sort_max){
                printf("%s\n",candidates[j].name);
            }
        }
    }
    return;
}

(3)runoff

#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
//voter:voter_count-ing
//rank:candidate_count-ing
bool vote(int voter, int rank, string name)
{
    // TODO
    for(int i=0;i<candidate_count;i++){
        if(!strcmp(name,candidates[i].name)){
            preferences[voter][rank] = i;
            return true;
        }
    }

    return false;
}

// Tabulate votes for non-eliminated candidates
void tabulate(void)
{
    // TODO
    int rank = 0;
    int number = 0;
    int count = 0;
    int mid = 0;
    while(count<voter_count){

        for(int j=0;j<voter_count;j++){
            rank = 0;
            for(int i=0;i<candidate_count;i++){
                mid = preferences[j][i];
                if(!candidates[mid].eliminated){
                    break;
                }
                rank ++;
            }
            number = preferences[j][rank];
            candidates[number].votes ++;
            count++;
        }
    }
    return;
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    // TODO
    int win_number = 0.5 * voter_count;
    int max = candidates[0].votes;
    int index = 0;
    for(int i=1;i<candidate_count;i++){
        if(candidates[i].votes > max){
            max = candidates[i].votes;
            index = i;
        }
        else if(candidates[i].votes == max){
            return false;
        }
        else{
            continue;
        }
    }
    int leader = candidates[index].votes;
    printf("leader-%i-%s\n",index,candidates[index].name);
    for(int i=0;i<candidate_count;i++){
        if(i==index){
            continue;
        }
        if(max==candidates[i].votes){
            return false;
        }
    }
    if(leader>win_number){
        printf("%s\n",candidates[index].name);
        return true;
    }
    else{
        return false;
    }
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    // TODO
    int min = candidates[0].votes;

    for(int j=1;j<candidate_count;j++){
        if(!candidates[j].eliminated){
            if(candidates[j].votes<min){
                min = candidates[j].votes;
            }
        }
    }
    return min;
}

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

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

    return;
}

(4)Tideman-ing