当前位置:网站首页>C语言初阶——牛客网精选好题
C语言初阶——牛客网精选好题
2022-07-01 05:35:00 【小吕同学lc】
BC68 X形图案
描述
KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的X形图案。
输入描述:
多组输入,一个整数(2~20),表示输出的行数,也表示组成“X”的反斜线和正斜线的长度。
输出描述:
针对每行输入,输出用“*”组成的X形图案。
#define _CRT_SECURE_NO_WARNINGS 1 //确保scanf函数能都在vs编译器下使用
#include <stdio.h>
int main()
{
char arr[20][20] = {
0 }; //创建二维数组
int i = 0;
int j = 0;
int a = 0;
while (scanf("%d", &a)!=EOF) //用循环来实现多组输入
{
for (i = 0;i < 20;i++) //将数组元素全部初始化为空格
{
for (j = 0;j < 20;j++)
{
arr[i][j] = ' ';
}
}
for (i = 0, j = 0;i < a, j < a;i++, j++) //正对角线元素改为‘*’
{
arr[i][j] = '*';
}
for (i = 0, j = a - 1;i < a, j >= 0;i++, j--) //反对角线元素改为‘*’
{
arr[i][j] = '*';
}
for (i = 0;i < a;i++) //打印出元素
{
for (j = 0;j < a;j++)
{
printf("%c", arr[i][j]);
}
printf("\n");
}
}
return 0;
}

BC54 获得月份天数
描述
KiKi想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。
输入描述:
多组输入,一行有两个整数,分别表示年份和月份,用空格分隔。
输出描述:
针对每组输入,输出为一行,一个整数,表示这一年这个月有多少天。
#define _CRT_SECURE_NO_WARNINGS 1 //确保scanf函数能都在vs编译器下使用
#include <stdio.h>
int is_leap_yeay(int x) // 判断年份是否为闰年
{
if (((x % 4 == 0) && (x % 100 != 0)) || x % 400 == 0)
return 29;
else
return 28;
}
int main()
{
int year = 0;
int month = 0;
int a = 0;
while (scanf("%d%d", &year, &month)!=EOF)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) // 大月为31天
{
printf("31\n");
}
else if (month == 2) //2月为特殊月,调用函数判断是否为闰年
{
int a = is_leap_yeay(year);
printf("%d\n", a);
}
else //其他情况为小月30天
{
printf("30\n");
}
}
return 0;
}
BC51 三角形判断
描述
KiKi想知道已经给出的三条边a,b,c能否构成三角形,如果能构成三角形,判断三角形的类型(等边三角形、等腰三角形或普通三角形)。
输入描述:
题目有多组输入数据,每一行输入三个a,b,c(0<a,b,c<1000),作为三角形的三个边,用空格分隔。
输出描述:
针对每组输入数据,输出占一行,如果能构成三角形,等边三角形则输出“Equilateral triangle!”,等腰三角形则输出“Isosceles triangle!”,其余的三角形则输出“Ordinary triangle!”,反之输出“Not a triangle!”。
#define _CRT_SECURE_NO_WARNINGS 1 //确保scanf函数能都在vs编译器下使用
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
while (scanf("%d%d%d", &a, &b, &c)!=EOF)
{
if ((a + b > c) && (a + c > b) && (b + c > a)) //判断是否为三角形
{
if ((a == b && b!=c )|| (b == c && c!=a)|| a == c && c!=b) //判断是否为等腰三角形
{
printf("Isosceles triangle!\n");
}
else if (a == b && b == c && c == a) //判断是否为等边三角形
{
printf("Equilateral triangle!\n");
}
else
{
printf("Ordinary triangle!\n");
}
}
else
{
printf("Not a triangle!");
}
}
return 0;
}
边栏推荐
- Lock free concurrency of JUC (leguan lock)
- Floweable source code annotation (40) class delegation
- Web Security (x) what is OAuth 2.0?
- JDBC common interview questions
- 移动端常用解决方案
- [excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
- RecycleView的一些使用
- In depth understanding of condition source code interpretation and analysis of concurrent programming
- QT waiting box production
- Summary of spanner's paper
猜你喜欢

Usage and principle of synchronized

CockroachDB: The Resilient Geo-Distributed SQL Database 论文阅读笔记

Explanation of characteristics of hydraulic slip ring

C WPF uses dockpanel to realize screenshot box

One click deployment of highly available emqx clusters in rainbow

Things generated by busybox

Multi table operation - foreign key cascade operation
SSM的教务管理系统(免费源码获取)

在Rainbond中一键部署高可用 EMQX 集群

How to select conductive slip ring material
随机推荐
One click deployment of highly available emqx clusters in rainbow
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Daily code 300 lines learning notes day 11
Common solutions for mobile terminals
Simple implementation of database connection pool
Intelligent operation and maintenance: visual management system based on BIM Technology
在Rainbond中一键部署高可用 EMQX 集群
Data governance: data governance framework (Part I)
Ebpf cilium practice (2) - underlying network observability
小程序常用组件小结
Flutter can refresh data every time the interface comes in
Cockroachdb: the resistant geo distributed SQL database paper reading notes
What things you didn't understand when you were a child and didn't understand until you grew up?
Web Security (IX) what is JWT?
Idea start view project port
Is it safe for a novice to open a securities account?
导数的左右极限和左右导数的辨析
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
RecycleView的一些使用
JDBC common interview questions