C刷题(五)
🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷
🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷
💚BC41、你是天才吗
描述:
据说智商140以上者称为天才,KiKi想知道他自己是不是天才,请帮他编程判断。输入一个整数表示一个人的智商,如果大于等于140,则表明他是一个天才,输出“Genius”
输入描述:
多组输入,每行输入包括一个整数表示的智商
输出描述:
针对每行输入,输出“Genius”
示例:
输入:
160
输出:
Genius
代码:
//直接!=EOF
#include <stdio.h>
int main()
{
int a = 0;
while ((scanf("%d", &a)) != EOF)
{
if (a >= 140)
{
printf("Genius\n");
}
}
return 0;
}
//按位取反
#include <stdio.h>
int main()
{
int a = 0;
//~C语言中操作符 按位取反
while (~scanf("%d", &a))
{
if (a >= 140)
{
printf("Genius\n");
}
}
return 0;
}
结果:
扩展:
- 多组输入:一定要处理好多组数据的输入问题,然后考虑循环如何结束
- 多组输入可以考虑有两种方法来实现
🧡BC42、完美成绩
描述:
KiKi想知道他的考试成绩是否完美,请帮他判断。从键盘输入一个整数表示的成绩,编程判断成绩是否在90~100之间,如果是则输出“Perfect”
输入描述:
多组输入,每行输入包括一个整数表示的成绩(90~100)
输出描述:
针对每行输入,输出“Perfect”
示例:
输入:
98
输出:
Perfect
代码:
//直接!=EOF
#include <stdio.h>
int main()
{
int score = 0;
while ((scanf("%d", &score)) != EOF)
{
if (score >= 90 && score <= 100)
{
printf("Perfect\n");
}
}
return 0;
}
按位取反
#include <stdio.h>
int main()
{
int score = 0;
while (~scanf("%d", &score))
{
if (score >= 90 && score <= 100)
{
printf("Perfect\n");
}
}
return 0;
}
结果:
扩展:
- 多组输入
- 在C语言中不要这样写90<=score<=100,会有bug
💛BC43、及格分数
描述:
KiKi想知道他的考试分数是否通过,请帮他判断。从键盘任意输入一个整数表示的分数,编程判断该分数是否在及格范围内,如果及格,即:分数大于等于60分,是输出“Pass”,否则,输出“Fail”
输入描述:
多组输入,每行输入包括一个整数表示的分数(0~100)
输出描述:
针对每行输入,输出“Pass”或“Fail”
示例:
输入:
94
输出:
Pass
输入:
44
输出:
Fail
代码:
#include <stdio.h>
int main()
{
int score = 0;
while (scanf("%d", &score) != EOF)
{
if (score >= 0 && score < 60)
{
printf("Fail\n");
}
else
{
printf("Pass\n");
}
}
return 0;
}
结果:
💙BC44、判断整数的奇偶性
描述:
KiKi想知道一个整数的奇偶性,请帮他判断。从键盘任意输入一个整数(范围-2^31~2^31-1),编程判断它的奇偶性
输入描述:
多组输入,每行输入包括一个整数
输出描述:
针对每行输入,输出该数是奇数(Odd)还是偶数(Even)
示例:
输入:
4
7
输出:
Even
Odd
代码:
#include <stdio.h>
int main()
{
int a = 0;
while (~scanf("%d", &a))
{
if (a % 2 == 0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
}
return 0;
}
结果:
💜BC45、最高分数
描述:
KiKi参加了语文、数学、外语的考试,请帮他判断三科中的最高分。从键盘任意输入三个整数表示的分数,编程判断其中的最高分
输入描述:
多组输入,每行输入包括三个整数表示的分数(0~100),用空格分隔
输出描述:
针对每行输入,输出为一行,即三个分数中的最高分
示例:
输入:
94 98 99
100 88 60
输出:
99
100
代码:
//创建3个变量
#include <stdio.h>
int main()
{
int chinese = 0;
int math = 0;
int english = 0;
while (~scanf("%d%d%d", &chinese, &math, &english))
{
int max = chinese > math ? chinese : math;
max = max > english ? max : english;
printf("%d\n", max);
}
return 0;
}
//创建数组,遍历数组
#include <stdio.h>
int main()
{
int score[3] = { 0 };
while (~scanf("%d%d%d", &score[0], &score[1], &score[2]))
{
int i = 0;
int max = 0;
for (i = 0; i < 3; i++)
{
if (max <= score[i])
{
max = score[i];
}
}
printf("%d\n", max);
}
return 0;
}
结果:
扩展:
- 多组输入(一行输入多个数据)
- 假设用数组创建变量的方式,max初值为0(成绩不为负数)
🤎BC46、判断元音还是辅音
描述:
KiKi开始学习英文字母,BoBo老师告诉他,有五个字母A(a), E(e), I(i), O(o),U(u)称为元音,其他所有字母称为辅音,请帮他编写程序判断输入的字母是元音(Vowel)还是辅音(Consonant)
输入描述:
多组输入,每行输入一个字母
输出描述:
针对每组输入,输出为一行,如果输入字母是元音(包括大小写),输出“Vowel”,如果输入字母是非元音,输出“Consonant”
示例:
输入:
A
b
输出:
Vowel
Consonant
代码:
//方法一
#include <stdio.h>
int main()
{
char ch = 0;
//存放元音字母的数组
char vowel[] = "AaEeIiOoUu";
while ((ch = getchar()) != EOF)
{
getchar();//处理\n(换行)
//判断
int i = 0;
for (i = 0; i < 10; i++)
{
if (ch == vowel[i])
{
printf("Vowel\n");
break;//跳出for循环
}
}
//要么已经是元音字母跳到这里
//要么遍历完整个数组,此时i = 10,打印辅音
if (i == 10)
{
printf("Consonant\n");
}
}
return 0;
}
//方法二
//使用库函数strchr(在字符串中查找字符)
//如果出现返回在字符串中的地址
//如果没有出现返回NULL(空指针)
#include <stdio.h>
#include <string.h>
int main()
{
char ch = 0;
//存放元音字母的数组
char vowel[] = "AaEeIiOoUu";
while ((ch = getchar()) != EOF)
{
getchar();//处理\n(换行)
//判断
if (strchr(vowel, ch))
{
printf("vowel\n");
}
else
{
printf("Consonant\n");
}
}
return 0;
}
//方法三
#include <stdio.h>
#include <string.h>
int main()
{
char ch = 0;
//存放元音字母的数组
char vowel[] = "AaEeIiOoUu";
//在%c前面加上空格,会消化掉前面所有的空白字符,读取最后一个字符
while (scanf(" %c",&ch) != EOF)
{
//判断
if (strchr(vowel, ch))
{
printf("vowel\n");
}
else
{
printf("Consonant\n");
}
}
return 0;
}
结果:
扩展:
- strchr函数,在字符串中查找字符。如果找到,返回在字符串中的地址,如果没有找到返回NULL(空指针)。
- 关于如何处理读取字符时,处理\n(换行)的问题:
- 利用getchar()读取\n
- 在使用scanf时,在%c前面加上空格,会消化掉前面所有的空白字符,读取一个字符,scanf(" %c",&ch)
🖤BC47、判断是不是字母
描述:
KiKi想判断输入的字符是不是字母,请帮他编程实现
输入描述:
多组输入,每一行输入一个字符
输出描述:
针对每组输入,输出单独占一行,判断输入字符是否为字母,输出内容详见输出样例
示例:
输入:
A
6
输出:
A is an alphabet.
6 is not an alphabet.
代码:
//方法一
#include <stdio.h>
int main()
{
int ch = 0;
while (scanf(" %c", &ch) != EOF)
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is an alphabet.\n",ch);
}
else
{
printf("%c is not an alphabet.\n",ch);
}
}
return 0;
}
//方法二
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch = 0;
while (scanf(" %c", &ch) != EOF)
{
if (isalpha(ch))
{
printf("%c is an alphabet.\n",ch);
}
else
{
printf("%c is not an alphabet.\n",ch);
}
}
return 0;
}
结果:
扩展:
isalpha函数判断一个字符是不是字母,如果是字母返回一个非0的值,如果不是字母返回0。
💚BC48、字母大小写转换
描述:
KiKi想完成字母大小写转换,有一个字符,判断它是否为大写字母,如果是,将它转换成小写字母;反之则转换为大写字母
输入描述:
多组输入,每一行输入一个字母
输出描述:
针对每组输入,输出单独占一行,输出字母的对应形式
示例:
输入:
a
A
Z
输出:
A
a
z
代码:
//方法一
#include <stdio.h>
int main()
{
int ch = 0;
while (scanf(" %c", &ch) != EOF)
{
if (ch >= 'A' && ch <= 'Z')
{
ch = ch + 32;
printf("%c\n", ch);
}
else if(ch>='a'&&ch<='z')
{
ch = ch - 32;
printf("%c\n", ch);
}
}
return 0;
}
//方法二 利用库函数
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch = 0;
while (scanf(" %c", &ch) != EOF)
{
if (isupper(ch))//判断是不是大写字母
{
printf("%c\n", tolower(ch));//大写字母转小写字母
}
else if(islower(ch))//判断是不是小写字母
{
printf("%c\n", toupper(ch));//小写字母转大写字母
}
}
return 0;
}
结果:
扩展:
isupper判断是不是大写字母
tolower大写字母转换为小写字母
islower判断是不是小写字母
toupper小写字母转换为大写字母
🧡BC50、计算单位阶跃函数
描述:
KiKi最近学习了信号与系统课程,这门课里有一个非常有趣的函数,单位阶跃函数,其中一种定义方式为:
现在试求单位冲激函数在时域t上的值
θ ( t ) = { 1 t > 0 1 / 2 t = 0 0 t < 0 \theta(t)=\begin{cases} 1 & t>0\\ 1/2 & t = 0 \\ 0 & t<0 \end{cases} θ(t)=⎩⎪⎨⎪⎧11/20t>0t=0t<0
输入描述:
题目有多组输入数据,每一行输入一个t(-1000<t<1000)表示函数的时域t
输出描述:
输出函数的值并换行
示例:
输入:
11
0
-11
输出:
1
0.5
0
代码:
#include <stdio.h>
int main()
{
int t = 0;
while (~scanf("%d", &t)!=EOF)
{
if (t > 0)
{
printf("1\n");
}
else if (t < 0)
{
printf("0\n");
}
else
{
printf("0.5\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!”
示例:
输入:
2 3 2
3 3 3
输出:
Isosceles triangle!
Equilateral triangle!
代码:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
while (~scanf("%d%d%d", &a, &b, &c))
{
if ((a + b > c) && (a + c > b) && (a + c) > b)
{
if (a == b && b == c)
{
printf("Equilateral triangle!\n");
}
else if (((a == b) && a != c) || ((b == c) && b != a) || ((a == c) && a != b))
{
printf("Isosceles triangle!\n");
}
else
{
printf("Ordinary triangle!\n");
}
}
else
{
printf("Not a triangle!\n");
}
}
return 0;
}
结果:
离开前,别忘了👍关注💡收藏💖
希望本文能够对大家有帮助~!
【C语言】带你用最短的时间刷题(附解题思路、具体代码)不断更新(一)
【C语言】带你用最短的时间刷题(附解题思路、具体代码)不断更新(二)