Skip to content
Ziwen's Profile
Go back

CS50 Problem Set 1

Edit page

Table of contents

Open Table of contents

CS50 Problem Set 1

Here’s my answer for the CS50 Problem Set 1. Hope that will help you a bit.

Problem 1: Hello, It’s Me

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

int main(void)
{
    string name = get_string("What's your name?\n");
    printf("Hello, %s\n", name);
    return 0;
}hello.c

Problem 2: Mario

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

void mario(int n);

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while (n < 1 || n > 8);
    mario(n);
    return 0;
}

void mario(int n)
{
    for (int i = 1; i < n + 1; i++)
    {
        for (int j = 0; j < n - i; j++)
        {
            printf(" ");
        }

        for (int k = 0; k < i; k++)
        {
            printf("#");
        }

        printf("  ");

        for (int k = 0; k < i; k++)
        {
            printf("#");
        }
        //        for (int j = 0; j < n - i; j++)
        //        {
        //            printf(" ");
        //        }

        printf("\n");
    }
}mario.c

Problem 3: Cash

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

int main(void)
{
    int cash;
    do
    {
        cash = get_int("Change owed: ");
    }
    while (cash < 0);

    int n_25 = 0;
    int n_10 = 0;
    int n_5 = 0;
    int n_1 = 0;
    int sum = 0;

    while (cash > 24)
    {
        cash -= 25;
        n_25 += 1;
    }
    // printf("%d\n", n_25);

    while (cash > 9)
    {
        cash -= 10;
        n_10 += 1;
    }
    // printf("%d\n", n_10);

    while (cash > 4)
    {
        cash -= 5;
        n_5 += 1;
    }
    // printf("%d\n", n_5);

    while (cash > 0)
    {
        cash -= 1;
        n_1 += 1;
    }
    // printf("%d\n", n_1);
    sum = n_25 + n_10 + n_5 + n_1;
    printf("%d\n", sum);
    return 0;
}cash.c

Edit page
Share this post on:

Previous Post
CS50 Problem Set 2
Next Post
我的第一篇文章