[CS50作业2023]Week1 – C

Problem Set 1

Hello

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

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


this version of Mario
 if feeling more comfortable:

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

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

    for (int i = 0; i < H; i++)
    {
        for (int m = H - i - 1; m != 0; m--)
        {
            printf(" ");
        }
        for (int j = H - i - 1; j < H; j++)
        {
            printf("#");
        }
        printf("  ");
        for (int n = H - i - 1; n < H; n++)
        {
            printf("#");
        }
        printf("\n");
    }
}

Credit if feeling more comfortable:

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

int main(void)
{
    long number;
    number = get_long("Number: ");
    int I = number / 10.e14;
    int A = number / 10.e13;
    int V = number / 10.e11;
    int A_1 = number / 10000000000000 % 10;
    int M_1 = number / 100000000000000 % 10;
    if (I == 4)
    {
        int n16 = number % 10;
        int n15 = number / 10 % 10;
        int n14 = number / 100 % 10;
        int n13 = number / 1000 % 10;
        int n12 = number / 10000 % 10;
        int n11 = number / 100000 % 10;
        int n10 = number / 1000000 % 10;
        int n9 = number / 10000000 % 10;
        int n8 = number / 100000000 % 10;
        int n7 = number / 1000000000 % 10;
        int n6 = number / 10000000000 % 10;
        int n5 = number / 100000000000 % 10;
        int n4 = number / 1000000000000 % 10;
        int n3 = number / 10000000000000 % 10;
        int n2 = number / 100000000000000 % 10;
        int n1 = number / 1000000000000000 % 10;
        int m15 = n15 * 2;
        if (m15 > 9)
        {
            m15 = m15 % 10 + m15 / 10;
        }
        int m13 = n13 * 2;
        if (m13 > 9)
        {
            m13 = m13 % 10 + m13 / 10;
        }
        int m11 = n11 * 2;
        if (m11 > 9)
        {
            m11 = m11 % 10 + m11 / 10;
        }
        int m9 = n9 * 2;
        if (m9 > 9)
        {
            m9 = m9 % 10 + m9 / 10;
        }
        int m7 = n7 * 2;
        if (m7 > 9)
        {
            m7 = m7 % 10 + m7 / 10;
        }
        int m5 = n5 * 2;
        if (m5 > 9)
        {
            m5 = m5 % 10 + m5 / 10;
        }
        int m3 = n3 * 2;
        if (m3 > 9)
        {
            m3 = m3 % 10 + m3 / 10;
        }
        int m1 = n1 * 2;
        if (m1 > 9)
        {
            m1 = m1 % 10 + m1 / 10;
        }
        int sum_1 = m1 + m3 + m5 + m7 + m9 + m11 + m13 + m15;
        int sum_2 = n16 + n14 + n12 + n10 + n8 + n6 + n4 + n2;
        int sum = sum_1 + sum_2;
        if (sum % 10 == 0)
        {
            printf("VISA\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else if ((I == 2 || I == 5) && (M_1 == 1 || M_1 == 2 || M_1 == 3 || M_1 == 4 || M_1 == 5))
    {

        int n16 = number % 10;
        int n15 = number / 10 % 10;
        int n14 = number / 100 % 10;
        int n13 = number / 1000 % 10;
        int n12 = number / 10000 % 10;
        int n11 = number / 100000 % 10;
        int n10 = number / 1000000 % 10;
        int n9 = number / 10000000 % 10;
        int n8 = number / 100000000 % 10;
        int n7 = number / 1000000000 % 10;
        int n6 = number / 10000000000 % 10;
        int n5 = number / 100000000000 % 10;
        int n4 = number / 1000000000000 % 10;
        int n3 = number / 10000000000000 % 10;
        int n2 = number / 100000000000000 % 10;
        int n1 = number / 1000000000000000 % 10;
        int m15 = n15 * 2;
        if (m15 > 9)
        {
            m15 = m15 % 10 + m15 / 10;
        }
        int m13 = n13 * 2;
        if (m13 > 9)
        {
            m13 = m13 % 10 + m13 / 10;
        }
        int m11 = n11 * 2;
        if (m11 > 9)
        {
            m11 = m11 % 10 + m11 / 10;
        }
        int m9 = n9 * 2;
        if (m9 > 9)
        {
            m9 = m9 % 10 + m9 / 10;
        }
        int m7 = n7 * 2;
        if (m7 > 9)
        {
            m7 = m7 % 10 + m7 / 10;
        }
        int m5 = n5 * 2;
        if (m5 > 9)
        {
            m5 = m5 % 10 + m5 / 10;
        }
        int m3 = n3 * 2;
        if (m3 > 9)
        {
            m3 = m3 % 10 + m3 / 10;
        }
        int m1 = n1 * 2;
        if (m1 > 9)
        {
            m1 = m1 % 10 + m1 / 10;
        }
        int sum_1 = m1 + m3 + m5 + m7 + m9 + m11 + m13 + m15;
        int sum_2 = n16 + n14 + n12 + n10 + n8 + n6 + n4 + n2;
        int sum = sum_1 + sum_2;
        if (sum % 10 == 0)
        {
            printf("MASTERCARD\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else if (A == 3 && (A_1 == 4 || A_1 == 7))
    {
        int n15 = number % 10;
        int n14 = number / 10 % 10;
        int n13 = number / 100 % 10;
        int n12 = number / 1000 % 10;
        int n11 = number / 10000 % 10;
        int n10 = number / 100000 % 10;
        int n9 = number / 1000000 % 10;
        int n8 = number / 10000000 % 10;
        int n7 = number / 100000000 % 10;
        int n6 = number / 1000000000 % 10;
        int n5 = number / 10000000000 % 10;
        int n4 = number / 100000000000 % 10;
        int n3 = number / 1000000000000 % 10;
        int n2 = number / 10000000000000 % 10;
        int n1 = number / 100000000000000 % 10;
        int m14 = n14 * 2;
        if (m14 > 9)
        {
            m14 = m14 % 10 + m14 / 10;
        }
        int m12 = n12 * 2;
        if (m12 > 9)
        {
            m12 = m12 % 10 + m12 / 10;
        }
        int m10 = n10 * 2;
        if (m10 > 9)
        {
            m10 = m10 % 10 + m10 / 10;
        }
        int m8 = n8 * 2;
        if (m8 > 9)
        {
            m8 = m8 % 10 + m8 / 10;
        }
        int m6 = n6 * 2;
        if (m6 > 9)
        {
            m6 = m6 % 10 + m6 / 10;
        }
        int m4 = n4 * 2;
        if (m4 > 9)
        {
            m4 = m4 % 10 + m4 / 10;
        }
        int m2 = n2 * 2;
        if (m2 > 9)
        {
            m2 = m2 % 10 + m2 / 10;
        }
        int sum_1 = m2 + m4 + m6 + m8 + m10 + m12 + m14;
        int sum_2 = n15 + n13 + n11 + n9 + n7 + n5 + n3 + n1;
        int sum = sum_1 + sum_2;
        if (sum % 10 == 0)
        {
            printf("AMEX\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else if (V == 4)
    {
        int n13 = number % 10;
        int n12 = number / 10 % 10;
        int n11 = number / 100 % 10;
        int n10 = number / 1000 % 10;
        int n9 = number / 10000 % 10;
        int n8 = number / 100000 % 10;
        int n7 = number / 1000000 % 10;
        int n6 = number / 10000000 % 10;
        int n5 = number / 100000000 % 10;
        int n4 = number / 1000000000 % 10;
        int n3 = number / 10000000000 % 10;
        int n2 = number / 100000000000 % 10;
        int n1 = number / 1000000000000 % 10;
        int m12 = n12 * 2;
        if (m12 > 9)
        {
            m12 = m12 % 10 + m12 / 10;
        }
        int m10 = n10 * 2;
        if (m10 > 9)
        {
            m10 = m10 % 10 + m10 / 10;
        }
        int m8 = n8 * 2;
        if (m8 > 9)
        {
            m8 = m8 % 10 + m8 / 10;
        }
        int m6 = n6 * 2;
        if (m6 > 9)
        {
            m6 = m6 % 10 + m6 / 10;
        }
        int m4 = n4 * 2;
        if (m4 > 9)
        {
            m4 = m4 % 10 + m4 / 10;
        }
        int m2 = n2 * 2;
        if (m2 > 9)
        {
            m2 = m2 % 10 + m2 / 10;
        }
        int sum_1 = m2 + m4 + m6 + m8 + m10 + m12;
        int sum_2 = n1 + n3 + n5 + n7 + n9 + n11 + n13;
        int sum = sum_1 + sum_2;
        if (sum % 10 == 0)
        {
            printf("VISA\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

Lab 1: Population Growth

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

int main(void)
{
    // TODO: Prompt for start size
    int s;
    do
    {
        s = get_int("Start size:");
    }
    while (s < 9);
    // TODO: Prompt for end size
    int e;
    do
    {
        e = get_int("End size:");
    }
    while (e < s);
    // TODO: Calculate number of years until we reach threshold
    int y = 0;
    while (s < e)
    {
        s = s + (s/3) - (s/4);
        y++;
    }

    // TODO: Print number of years
    printf("Years: %i\n",y);
}

Week 1 Practice Problems
Half:

// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value

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

float half(float bill, float tax, int tip);

int main(void)
{
    float bill_amount = get_float("Bill before tax and tip: ");
    float tax_percent = get_float("Sale Tax Percent: ");
    int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
    float h = ((bill + bill * (tax / 100.0)) * (1 + tip / 100.0)) / 2.0;
    return h;
}


Prime:

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

bool prime(int number);

int main(void)
{
    int min;
    do
    {
        min = get_int("Minimum: ");
    }
    while (min < 1);

    int max;
    do
    {
        max = get_int("Maximum: ");
    }
    while (min >= max);

    for (int i = min; i <= max; i++)
    {
        if (prime(i))
        {
            printf("%i\n", i);
        }
    }
}

bool prime(int number)
{
    // TODO
    if (number == 1)
    {
        return false;
    }
    for (int j=2;j < number;j++)
    {
        if (number%j == 0)
        {
            return false;
        }
    }
    return true;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇