0
点赞
收藏
分享

微信扫一扫

鹏哥手把手带我刷好题 · 编程练习 · II



大家好,我是安然无虞。


目录

​​1.判断字母​​

​​2.字符圣诞树​​

​​3.ASCII码​​

​​4.出生日期的输入输出​​

​​5.2的n次方计算​​

​​6.按照格式输入并交换输出​​

​​7.字符转ASCII码​​

​​8.计算表达式的值​​

​​9.计算带余除法​​

​​10.计算体重指数​​

​​11.计算三角形的周长和面积​​

​​12.计算球体的体积​​

​​结语:遇见安然遇见你,不负代码不负卿!​​

【前言】


题目比较简单,130来题,二刷了,因为之前写的很多解法太繁琐,所以用几天的时间重新过一遍。 


鹏哥手把手带我刷好题 · 编程练习 · II_牛客网

1.判断字母


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_牛客网_02

代码执行:

代码1:

#include<stdio.h>
int main()
{
int ch = 0;
//多组输入
while((ch=getchar())!=EOF)
{

if(ch>='A' && ch<='Z' || ch>='a' && ch<='z')
{
printf("YES\n");
}
else
{
printf("NO\n");
}
getchar();//一定要记得清除缓冲区

}

return 0;
}

代码2:

//isplpha - 是专门用来判断一个字符是不是字母的库函数
//是字母则返回非0的值,不是字母则返回0
//需要引头文件#include<ctype.h>
#include<stdio.h>
#include<ctype.h>

int main()
{
int ch = 0;
//多组输入
while((ch = getchar())!=EOF)
{
//判断并输出
if(isalpha(ch))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
getchar();//清理缓冲区\n
}
return 0;
}

2.字符圣诞树

题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_03

示例:

输入:1
输出:
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1

代码执行:

#include<stdio.h>

int main()
{
char ch = 0;
ch = getchar();
int i = 0;
//每循环一次,打印一行
//每一行由两部分组成:空格和字符
for (i = 0; i < 5; i++)
{
//打印空格
int j = 0;
for (j = 0; j < 5 - 1 - i; j++)
{
printf(" ");
}
//打印字符
for (j = 0; j <= i; j++)
{
printf("%c ", ch);
}
printf("\n");
}
return 0;
}

3.ASCII码


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_04

代码执行:

#include<stdio.h>
int main()
{
char arr[] = {73,32,99,97,110,32,100,111,32,105,116,33};
//arr是一个数组,数组是使用下标来访问的
//计算数组元素多少
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for(i = 0; i < sz; i++)
{
printf("%c", arr[i]);
}
return 0;
}

4.出生日期的输入输出​



【敲黑板】:本题需要注意输入输出格式。 


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_05示例:

输入:20130225 
输出:
year=2013
month=02
date=25


注意: 

通过scanf函数的%m格式控制可以指定输入域宽,输入数据域宽(列数),按此宽度截取所需数据;通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。


代码执行:

#include<stdio.h>

int main()
{
int year = 0;
int month = 0;
int date = 0;
//按照格式输入
scanf("%4d%2d%2d", &year, &month, &date);
//输出
printf("year=%4d\n", year);
printf("month=%02d\n", month);
printf("date=%02d\n", date);
return 0;
}

5.2的n次方计算


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_#include_06

 大家想深入学习的话可以看看我这篇文章:

蓝桥杯算法竞赛系列第一章——位运算的奇巧淫技及其实战_安然无虞的博客:在接下来的两个月中,博主持续推出两个系列的博文,有关零基础搞定C语言,蓝桥杯算法竞赛,欢迎读者发表自己的想法,期待您的留言评论。

代码执行:

#include<stdio.h>

int main()
{
int n = 0;
//多组输入
while(scanf("%d", &n)!=EOF)
{
printf("%d\n", 1<<n);
}

return 0;
}

6.按照格式输入并交换输出


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_牛客网_07

示例:

输入:a=1,b=2
输出:a=2,b=1

代码执行:

#include<stdio.h>

int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("a=%d,b=%d", &a, &b);
//交换
//想象成一瓶酱油和一瓶醋,再拿一个空瓶进行交换
c = a;
a = b;
b = c;
//输出
printf("a=%d,b=%d\n", a, b);
return 0;
}

补充一道变态的笔试题:


不能创建临时变量(第三个变量),实现两个数的交换:

方法一:加减法

a = a + b;

b = a - b;

a = a - b;

这样就解决了,但是有不好的地方,如果a, b 很大,就可能会导致溢出,所以还有一种方法,就是利用异或法

方法二:异或法

a = a ^ b;

b = a ^ b;

a = a ^ b;

这样就交换完成了,但是在实际运用当中,习惯定义第三个变量来实现两个数的交换,因为鸭,上面的代码可读性都不高! 


7.字符转ASCII码


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_08示例:

输入:a
输出:97

代码执行:

#include<stdio.h>
int main()
{
char ch = 0;
ch = getchar();
printf("%d\n",ch);
return 0;
}

8.计算表达式的值


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_09

代码执行:

#include<stdio.h>
int main()
{
int a = 40;
int c = 212;
printf("%d\n", (-8+22)*a - 10 + c / 2);
return 0;
}

9.计算带余除法


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_#include_10

示例:

输入:15 2
输出:7 1

代码执行:

#include<stdio.h>

int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
// / 除法操作符 得到的是商
// % 取余(取模)操作符,得到的是余数
int c = a/ b;
int d = a % b;
printf("%d %d\n", c, d);
return 0;
}

10.计算体重指数


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_牛客网_11

示例:

输入:70 170
输出:24.22

代码执行:

#include<stdio.h>
int main()
{
int weight = 0;
int height = 0;
double bmi = 0.0;
//输入
scanf("%d %d", &weight, &height);
//计算BMI
//之所以除以100.0,是因为除号两边都是整型,要得到小数,必须保证两边有一个是浮点数
bmi = weight / ((height / 100.0) * (height / 100.0));
//输出
printf("%.2lf\n", bmi);
return 0;
}

11.计算三角形的周长和面积

题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_12

示例:

输入:3 3 3
输出:circumference=9.00 area=3.90

注意:给出三角形的三条边让我们计算三角形面积,那么我们需要运用海伦公式,题目就迎刃而解了。 

鹏哥手把手带我刷好题 · 编程练习 · II_ascii码_13

代码执行:

#include<stdio.h>
#include<math.h>

int main()
{
double a = 0.0;
double b = 0.0;
double c = 0.0;

double circumference = 0.0;//周长
double area = 0.0;//面积
//输入
scanf("%lf %lf %lf", &a,&b,&c);
//计算
circumference = a + b + c;
double p = (a + b + c) / 2;
area = sqrt(p * (p - a) * (p - b) * (p - c));
printf("circumference=%.2lf area=%.2lf\n",circumference, area);
return 0;
}

12.计算球体的体积


题目描述:

鹏哥手把手带我刷好题 · 编程练习 · II_#include_14

代码执行:

#include<stdio.h>
#include<math.h>
#include<math.h>
#define PI 3.1415926
int main()
{
double r = 0.0;
double v = 0.0;
//输入
scanf("%lf", &r);
//计算
v = 4.0 / 3 * PI * pow(r, 3);
//输出
printf("%.3lf\n", v);
return 0;
}

结语:遇见安然遇见你,不负代码不负卿!


鹏哥手把手带我刷好题 · 编程练习 · II_c语言_15求求了,来个三连吧。



举报

相关推荐

0 条评论