当前位置:网站首页>带你刷(牛客网)C语言百题(第三天)
带你刷(牛客网)C语言百题(第三天)
2022-07-21 23:08:00 【@每天都要敲代码】
作者简介:大家好我是@每天都要敲代码,一位材料转码农的选手,希望一起努力,一起进步!
个人主页:@每天都要敲代码的个人主页
系列专栏:牛客网C语言刷题专栏
推荐一款模拟面试、刷题神器,从基础到大厂面试题点击跳转刷题网站进行注册学习
目录
习题一:出生日期输入输出_牛客题霸_牛客网
描述
输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。
数据范围:年份满足 1990≤y≤2015 ,月份满足 1≤m≤12 ,日满足1≤d≤30
输入描述:
输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。
输出描述:
三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。
输入:20130225
输出:
year=2013
month=02
date=25备注:通过scanf函数的%m格式控制可以指定输入域宽,输入数据域宽(列数),按此宽度截取所需数据;通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。
具体代码:
#include <stdio.h>
int main()
{
// 方法1:看成三个数
int y=0,m=0,d=0;
scanf("%4d%2d%2d",&y,&m,&d);
//指定带宽
printf("year=%4d\nmonth=%02d\ndate=%02d\n",y,m,d);
//方法2:看成一个数
int date = 0;
scanf("%d",&date);
printf("year=%4d\nmonth=%02d\ndate=%02d\n",date/10000,date/100%100,date%100);
return 0;
}
习题二:按照格式输入并交换输出_牛客题霸_牛客网
描述
输入两个整数,范围
~
,交换两个数并输出。
输入描述:
输入只有一行,按照格式输入两个整数,范围,中间用“,”分隔。
输出描述:
把两个整数按格式输出,中间用“,”分隔。
输入:a=1,b=2
输出:a=2,b=1
#include <stdio.h>
int main(){
int a,b;
scanf("a=%d,b=%d",&a,&b);
// 方法1:借用临时变量
int tmp = a;
a = b;
b = tmp;
printf("a=%d,b=%d",a,b);
// 方法2:使用+,有可能溢出
a = a+b;
b = a-b;
a = a-b;
printf("a=%d,b=%d",a,b);
// 方法3:使用异或^
a = a^b;
b = a^b;
a = a^b;
printf("a=%d,b=%d",a,b);
return 0;
}
习题三:大小写转换_牛客题霸_牛客网
描述
实现字母的大小写转换。多组输入输出。
输入描述:
多组输入,每一行输入大写字母。
输出描述:
针对每组输入输出对应的小写字母。
输入:
A
B
输出:
a
b
#include <stdio.h>
#include <stdlib.h>
int main(){
// 1.使用库函数tolower
int ch = 0;
while((ch = getchar()) != EOF){ //多组输入,一般都是写成这个格式
printf("%c",tolower(ch)); //使用tolower库函数
}
// 2.不使用库函数
while((ch = getchar()) != EOF){
putchar(ch+32); //大写和小写差32,例如:A是65,a是97
getchar(); //吸收遗留的\n
printf("\n"); //换行
}
return 0;
}
习题四:十六进制转十进制_牛客题霸_牛客网
描述
BoBo写了一个十六进制整数ABCDEF,他问KiKi对应的十进制整数是多少。
输入描述:
无
输出描述:
十六进制整数ABCDEF对应的十进制整数,所占域宽为15。
备注:
printf可以使用使用格式控制串“%md”输出域宽为m的十进制整数。
#include<stdio.h>
int main(){
printf("%15d\n",0XABCDEF); //十六进制数是以0X开头的
return 0;
}
习题五:缩短二进制_牛客题霸_牛客网
描述
我们处理的整数通常用十进制表示,在计算机内存中是以二进制补码形式存储,但通常二进制表示的整数比较长,为了便于在程序设计过程中理解和处理数据,通常采用八进制和十六进制,缩短了二进制补码表示的整数,但保持了二进制数的表达特点。请输出十进制整数1234对应的八进制和十六进制。
输入描述:
无
输出描述:
十进制整数1234对应的八进制和十六进制(字母大写),用空格分开,并且要求,在八进制前显示前导0,在十六进制数前显示前导0X。
备注:
printf可以使用使用格式控制串“%o”、“%X”分别输出八进制整数和十六进制整数,并使用修饰符“#”控制前导显示
#include <stdio.h>
int main(){
// 输入十进制数
int n = 1234;
scanf("%d",&n);
// 打对应的八进制和十六进制
printf("%#o %#X",n,n); //#在输出打印的时候会显示八进制前面的0,和十六进制前面的0X
}
习题六:牛牛的空格分隔_牛客题霸_牛客网
描述
牛牛从键盘读入一个字符,一个整数,一个单精度浮点数,按顺序输出它们,并用空格分隔,浮点数保留 6 位小数。
输入描述:
读入一个字符,一个整数,一个单精度浮点数用换行符隔开,
输出描述:
按顺序输出字符、整数、单精度浮点数,用空格分隔,浮点数保留 6 位小数
输入:
a
1
1.23
输出:
a 1 1.230000
#include <stdio.h>
int main(){
char c;
int i;
float f;
scanf("%c\n%d\n%f\n",&c,&i,&f);
printf("%c %d %.6f",c,i,f);
return 0;
}
结束语
今天的分享就到这里啦!快快通过下方链接注册加入刷题大军吧!各种大厂面试真题在等你哦!
刷题神器,从基础到大厂面试题点击跳转刷题网站
边栏推荐
- The point rotates clockwise around the center of the grid by 90 ° 180 ° 270 ° coordinate change
- QT uses Google breakpad to capture program crash reports (dump files)
- Simple crud of SSM
- 图表绘制总结
- pyhton爬取一、二级网站页面,并将爬取的图片信息保存到本地
- Mysql8 stored procedure generates calendar table and exception handling
- QT creator shortcut keys, with shortcut key configuration method
- QT program packaging and publishing method (using the official windeployqt tool)
- 智能仪器仪表行业数字化供应链管理系统:加速企业智慧供应链平台转型
- Typescript - syntax introduction
猜你喜欢
Digital supply chain management system for intelligent instrument industry: accelerating the transformation of enterprise intelligent supply chain platform
ClickHouse的安装
service(lb) 和管理的pod
恭賀《創新·賦能》產品創新管理論壇7月16日成功召開
Join hands with HMS core analysis services to help the efficient growth of games with data
How to select the type of ankerui intelligent miniature circuit breaker?
Félicitations pour la tenue réussie du Forum sur la gestion de l'innovation de produits « innovation et autonomisation » le 16 juillet
数商云:供应商多场景趋势下,服装企业如何打造灵活应用的SRM管理体系?
企业数字化办公选SaaS服务还是私有化服务?
SSM之简单的CRUD
随机推荐
Mysql8 stored procedure generates calendar table and exception handling
The point rotates clockwise around the center of the grid by 90 ° 180 ° 270 ° coordinate change
The LAAS solution of elephant swap has risen rapidly and built a new defi2.0 protocol
【C语言进阶】关于柔性数组的学习
“新能源+储能“从数字孪生开始,图扑将智慧电力做到极致
Qt开发应用程序Debug与Release设置
uniapp常用的生命周期
Border dynamic effect implementation
[MdSQL]表的增删查改(进阶)
v7底部栏fragment
MySQL: character sets and comparison rules
JVM parent delegation mechanism
Different accounts are logged in in different windows of the same browser. When the window is switched, the page refresh account is changed to the last login account
Qscriptengine official documentation
One bite of Stream(8)
《倍增商业成功宝典》全新升级上线!炙夏新品,久等终至!
Elephant Swap的LaaS方案迅速崛起,构建全新DeFi2.0协议
How does Siemens PLC in the factory control room collect the production data of multiple production lines in a centralized and wireless way?
工厂控制室西门子PLC如何集中无线采集多条产线生产数据?
2022/07/18------顺时针打印矩阵