CS50 第一周 C

hello.c

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

int main()
{
   string str = get_string("What is your name? ");
   printf("Hello,%s!\n");
   system("pause");
   
   return 0;
}

prime.c

任何合数n都至少有一个不超过\sqrt{n}的素因子

n > 1 是合数,则存在素数p,使得p<=\sqrt{n}

证明:【素数】算术基本定理,欧几里得定理,很简单(里面的证明有点硬核,但也不难理解)_哔哩哔哩_bilibili

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

bool is_prime(int num);

int main(void)
{
    int min = get_int("最小值: ");
    int max = get_int("最大值: ");
    for (int i = min; i <= max; i++)
    {
        // 判断是否是素数
        if (is_prime(i))
        {
            // 如果是,打印出来
            printf("%d\n", i);
        }
    }

    system("pause");
    return 0;
}

bool is_prime(int num)
{
    if (num < 2)
    {
        return false;
    }
    for (int i = 2; i*i <= num; i++)
    {
        if (num % i == 0)
        {
            return false;
        }
    }

    return true;
}

population.c

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
//羊驼每年新生1/3,死亡1/4
//比如12只羊驼,一年后,新生4只,死亡3只,总数13只
//只有整除,有小数点的全部舍弃

int get_year(int start_size,int end_size);

int main(void)
{
    // TODO: Prompt for start size
    int start_size = get_int("初始羊驼数量: ");
    // TODO: Prompt for end size
    int end_size = get_int("最终羊驼数量: ");
    // TODO: 计算达到最终羊驼规模所需年数
    int year = get_year(start_size, end_size);
    // TODO: Print number of years
    printf("%d\n",year);

    system("pause");
    return 0;
}

int get_year(int start_size,int end_size)
{
    int y = 0;
    int count = start_size;
    while (count < end_size)
    {
        start_size = count;
        count = start_size +(start_size/3) - (start_size/4);
        y++;
    }
    return y; 
}

mario_less.c

//流程思维

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

int main()
{
    // 获取网格大小 Get size of grid
    // 介于1-8之间的整数
    int n;
    do
    {
        n = get_int("Size: \n");
    } while (n < 1 || n > 8);
    // 打印网格砖块 Print grid of bricks
    // 要求第一行打印最后1个,第二行打印最后两个,
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (j < (n - i - 1))
            {
                printf(" ");
            }
            else
            {
                printf("#");
            }
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

mario_more.c

//结构思维

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

/*  要求: 介于1-8之间的整数
    比如打印高度:4
       #  #
      ##  ##
     ###  ###
    ####  #### 
*/

void left_grid(int n, int i);
void right_grid(int i);

int main()
{
    // 获取网格高度 Get Height of grid
    int n;
    do
    {
        n = get_int("高度: \n");
    } while (n < 1 || n > 8);
    // 打印网格砖块 Print grid of bricks
    for (int i = 0; i < n; i++)
    {
        left_grid(n, i);
        printf(" ");
        right_grid(i);
        printf("\n");
    }
    system("pause");
    return 0;
}

void left_grid(int n, int i)
{
    for (int j = 0; j < n; j++)
    {
        if (j < (n - i - 1))
        {
            printf(" ");
        }
        else
        {
            printf("#");
        }
    }
}

void right_grid(int i)
{
    for (int j = 0; j <= i; j++)
    {
        printf("#");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anxonz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值