【C算法】编程初学者入门训练140道(1~20)

链接: 牛客编程初学者入门训练150题
希望大家学习和纠错。

BC1 实践出真知

链接: 实践出真知

【说明】
(1)printf的使用

#include <stdio.h>
 
int main(void) 
{
    printf("Practice makes perfect!\n");
     
    return 0;
}

BC2 我是大V

链接: 我是大V

【说明】
(1)\n的使用

#include <stdio.h>
 
int main() {
   printf("v   v\n v v\n  v\n");
    return 0;
}

BC3 有容乃大

链接: 有容乃大

【说明】
(1)打印sizeof()类型的值,使用%zd转换说明

#include<stdio.h>

int main()
{
    printf("The size of short is %zd bytes.\n",sizeof(short));
    printf("The size of int is %zd bytes.\n",sizeof(int));
    printf("The size of long is %zd bytes.\n",sizeof(long));
    printf("The size of long long is %zd bytes.\n",sizeof(long long));
    return 0;
}

BC6 小飞机

链接: 小飞机

【说明】
(1)注意对齐

#include <stdio.h>

int main()
{
    printf("     **\n");
    printf("     **\n");
    printf("************\n");
    printf("************\n");
    printf("    *  *\n");
    printf("    *  *\n");
    return 0;
}

BC7 缩短二进制

链接: 缩短二进制

【说明】
(1)以十进制显示数字,使用%d;以八进制显示数字,使用%o;以十六进制显示数字,使用%x(大写使用%X);
(2)要显示各进制的前缀0、0x或者0X,必须使用%#o、%#x、%#X

#include <stdio.h>

int main()
{
    printf("%#o %#X",1234 ,1234);
    return 0;
}

BC8 十六进制转十进制

链接: 十六进制转十进制

【说明】
(1)printf可以使用使用格式控制串“%md”输出域宽为m的十进制整数。
(2)十六进制的数字需要在前面+0x

#include <stdio.h>

int main()
{
    printf("%15d",0xABCDEF);
    return 0;
}

BC9 printf的返回值

链接: printf的返回值

【说明】
(1)printf的返回值是printf打印出的数据的个数

#include <stdio.h>

int main()
{
    printf("\n%d",printf("Hello world!"));
    return 0;
}

BC10 成绩输入输出

链接: 成绩输入输出

【说明】
(1)使用scanf与printf进行输入输出

#include <stdio.h>

int main()
{
    int grade1, grade2, grade3;
    scanf("%d %d %d", &grade1, &grade2, &grade3);
    printf("score1=%d,score2=%d,score3=%d", grade1, grade2, grade3);
    return 0;
}

BC11 学生基本信息输入输出

链接: 学生基本信息输入输出

【说明】
(1)注意使用scanf输入时需要分号与逗号

#include <stdio.h>

int main()
{
    int student_ID;
    float C_language, math, english;
    scanf("%d;%f,%f,%f", &student_ID, &C_language, &math, &english);
    printf("The each subject score of No. %d is %.2f, %.2f, %.2f.", student_ID, C_language, math, english);

    return 0;
}

BC12 字符圣诞数

链接: 字符圣诞树

【说明】
(1)利用多重for循环

#include <stdio.h>

int main()
{
    char ch;
    ch = getchar();
    for(int i = 1; i <= 5; ++i)
    {
        for(int j = 1; j <= (5-i); ++j)
        {
            printf(" ");
        }
        for(int k = 1; k <= (i); ++k)
        {
            printf("%c", ch);
            printf(" ");
        }
        printf("\n");
    }
    return 0;
}

BC13 ASCII 码

链接: ASCII 码

【说明】
(1)利用数组以及for循环求解

#include <stdio.h>

int main()
{
    int arr[] = {73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33};
    for(int i = 0; i < (sizeof(arr) / sizeof(arr[0])); ++i)
    {
        printf("%c",arr[i]);
    }
    return 0;
}

BC14 出生日期输入输出

链接: 出生日期输入输出

【说明】
(1)scanf输入时,转换说明前面可以添加数字修饰符,来显示最大字段宽度,输入达到最大字段宽度处。
(2)通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。对于数值格式,用前导0代替空格填充字段宽度。对于整数格式,如果出现-标记或者指定精度,则忽略该标记。

#include <stdio.h>

int main()
{
    int year, month, date;
    scanf("%4d %2d %2d", &year, &month, &date);
    printf("year=%04d\nmonth=%02d\ndate=%02d\n", year, month, date);
    return 0;
}

BC15 按照格式输入并交换输出

链接: 按照格式输入并交换输出

【说明】
(1)如果格式控制串中有非格式字符则输入时也要输入该非格式字符。

#include <stdio.h>

int main()
{
    int a,b;
    scanf("a=%d,b=%d", &a, &b);
    int tmp = a;
    a = b;
    b = tmp;
    printf("a=%d,b=%d", a, b);
    return 0;
}

BC16 字符转ASCII码

链接: 字符转ASCII码

【说明】
(1)字符在计算机中是以数字的形式的存储的。

#include <stdio.h>

int main()
{
    char ch;
    scanf("%c",&ch);
    printf("%d",ch);
    return 0;
}

BC17 计算表达式的值

链接: 计算表达式的值

【说明】
(1)printf后面的输出列表中可以出现表达式。

#include <stdio.h>

int main()
{
    int a = 40, c = 212;
    printf("%d",(-8+22)*a-10+c/2);
    return 0;
}

BC18 计算带余除法

链接: 计算带余除法

【说明】
(1)除法、求模

#include <stdio.h>

int main()
{
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d %d", a / b, a % b);
    
    return 0;
}

BC19 反向输出一个四位数

链接: 反向输出一个四位数

【说明】
(1)这里使用for循环进行反向输出

#include <stdio.h>

int main()
{
    int num;
    int n;
    scanf("%d", &num);
    for(int i = 0; i < 4; ++i)
    {
        n = num % 10;
        num /= 10;
        printf("%d",n);
    }
    return 0;
}

BC20 kiki算数

链接: kiki算数

【说明】
(1)熟练使用求模运算符

#include <stdio.h>

int main()
{
     int a, b;
     scanf("%d %d", &a , &b);
     a %= 100;
     b %= 100;
     int c = (a + b) % 100;
     printf("%d", c);
    return 0;
}
  • 12
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值