C语言刷题(3)——“C”
创始人
2024-05-27 04:11:17
0

各位CSDN的uu们你们好呀,今天小雅兰的内容还是做几道题噢,好好复习一下之前的知识点,现在,就让我们开始复习吧

牛客网在线编程_编程学习|练习题_数据结构|系统设计题库

倒置字符串_牛客题霸_牛客网


BC40 竞选社长

BC41 你是天才吗?

BC42 完美成绩

BC43 及格分数

BC44 判断整数奇偶性

BC45 最高分数

BC46 判断是元音还是辅音

BC47 判断是不是字母

BC48 字母大小写转换

BC49 判断两个数的大小关系

BC50 计算单位阶跃函数

BC51 三角形判断

BC52 衡量人体胖瘦程度

BC53 计算一元二次方程

BC54 获得月份天数

OR62 倒置字符串


 

 

此代码用到了gets这个库函数 

#include
int main()
{char buf[100] = { 0 };//输入gets(buf);int count_a = 0;int count_b = 0;int i = 0;while (buf[i] != '0'){if (buf[i] == 'A'){count_a++;}else if (buf[i] = 'B'){count_b++;}i++;}if (count_a > count_b){printf("A\n");}else if (count_a < count_b){printf("B\n");}else if(count_a==count_b){printf("E\n");}return 0;
}

这段代码还可以再改进一下,可不可以不定义两个变量呢?这当然也是可以的。

#include
int main()
{char arr[100]= { 0 };gets(arr);int i = 0 ;int flag = 0 ;while( arr[i] != '0'){if(arr[i] =='A'){flag++;}else if(arr[i] =='B'){flag--;}i++;}if (flag > 0){printf("A");}else if (flag < 0){printf("B");}else{printf("E");}return 0;
}

那么只定义一个变量的方法我们也掌握了,那么,还有没有其他的写法呢?比如不用gets,这当然也是可以的。

下面这段代码使用了getchar

 

getchar拓展_认真学习的小雅兰.的博客-CSDN博客

#include int main()
{char arr[100] = { 0 };int ch = 0 ;int flag = 0 ;//如果getchar获取了while (((ch= getchar()) != '0') && ch!= EOF){if(ch=='A'){flag++;}else if(ch=='B'){flag--;}}if (flag > 0){printf("A");}else if (flag < 0){printf("B");}else{printf("E");}return 0;
}

1. 本题有很多解法。

2. 主要就是读取输入数据的问题要解决好,输入有2个结束条件(遇到读取结束EOF,或者'0')。

3. 剩余的工作就是统计个数后,然后根据情况按照格式输出。

 

#include 
int main() 
{int a=0;while(scanf("%d",&a)!=EOF){if(a>=140){printf("Genius\n");}else{printf("不是天才\n");}}return 0;
}

这个代码还有另外一种写法:

#include
int main()
{int n = 0;//这种写法是因为scanf读取失败返回EOF,EOF是-1,所以按位取反后的结果是0,0为假,可以让循环停止。while (~scanf("%d", &n)){if (n >= 140){printf("Genius");}else{printf("不是天才\n");}}return 0;
}

关于对组输入的题目,一定要处理好多组数据的输入问题,然后考虑循环如何结束。

这里写到了一个操作符—— ~ 

 操作符的详细知识可以看小雅兰的博客噢

操作符——“C”_认真学习的小雅兰.的博客-CSDN博客_十进制数10可以表示为

整型提升+算术转换——“C”_认真学习的小雅兰.的博客-CSDN博客

#include int main()
{int score = 0;while (scanf("%d", &score) != EOF){if (score >= 90 && score <= 100)printf("Perfect");}return 0;
}

1. 多组输入。

2. 数学中的 90的写法,在C语言中直接写是有bug的。

 

#include int main()
{int score = 0;while (scanf("%d", &score) != EOF){if (score >= 60)printf("Pass\n");elseprintf("Fail\n");}return 0;
}

#include int main() 
{int i=0;while(scanf("%d",&i)!=EOF){if(i%2==0)printf("Even\n");else if(i%2!=0)printf("Odd\n");}return 0;
}

 这几道题目都是类似的。

 

#include
int main()
{int n1 = 0;int n2 = 0;int n3 = 0;while (scanf("%d %d %d", &n1, &n2, &n3) != EOF){int max = n1 > n2 ? n1 : n2;max = max > n3 ? max : n3;printf("%d\n", max);}return 0;
}

另一种写法:

#include int main()
{int i = 0;int score[3] = { 0 };while (scanf("%d %d %d", &score[0], &score[1], &score[2]) != EOF){int max = 0;//每一组测试,max都恢复到0int i = 0;for (i = 0; i < 3; i++){if (score[i] > max)max = score[i];}printf("%d\n", max);}return 0;
}

1. 多组输入

2. 每组输入接受3个数字,求出最大值,因为成绩不能是负数,所以假设max期初是0.

 

#include int main()
{char ch = 0;//存放元音字母的数组char arr[] = "AEIOUaeiou";//输入while ((ch = getchar()) != EOF){//判断int i = 0;for (i = 0; i < 10; i++){if (ch == arr[i]){printf("Vowel\n");break;}}if (i == 10)printf("Consonant\n");getchar();//去除每个字符后的\n}return 0;
}

当然,这个代码还可以用库函数——strchr

 

#include
#include
int main()
{int ch = 0;//存放元音字母的数组char vowel[] = "AEIOUaeiou";//输入while (ch = getchar() != EOF){//判断//strchr是用来判断ch是否在字符串vowel中出现//如果出现了,则返回在vowel字符串中的地址//如果没出现,则返回NULLif (strchr(vowel, ch)){printf("Vowel\n");}else{printf("Constantly\n");}getchar();//处理\n}return 0;
}

另一种写法:

#include int main()
{char ch = 0;char arr[] = "AEIOUaeiou";//在%c的前面写一个空格会消化掉前面所有的空白字符,然后读取一个字符while (scanf(" %c", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == arr[i]){printf("Vowel\n");break;}}if (i == 10)printf("Consonant\n");}return 0;
}

另一种写法:

#include
int main()
{char ch = 0;char arr[] = "AEIOUaeiou";//在%c的后边发给一个'\n',其实在输入时候就会消化掉这个\n字符//不会为下次留下空白字符的隐患while (scanf("%c\n", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == arr[i]){printf("Vowel\n");break;}}if (i == 10)printf("Consonant\n");}return 0;
}

#include int main()
{int ch = 0;while ((ch = getchar()) != 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);}//清理掉\ngetchar();}return 0;
}

当然,也可以用库函数,专门用来判断是不是字母——isalpha

#include int main()
{int ch = 0;while ((ch = getchar()) != EOF){if (isalpha(ch)){printf("%c is an alphabet.\n", ch);}else{printf("%c is not an alphabet.\n", ch);}//清理掉\ngetchar();}return 0;
}

 

#include 
#include
int main()
{int ch = 0;while ((ch = getchar()) != EOF){if (isalpha(ch)){printf("%c is an alphabet.\n", ch);}else{printf("%c is not an alphabet.\n", ch);}//清理掉\ngetchar();}return 0;
}

 

 

 

#include
int main()
{char ch = 0;while (scanf("%c", &ch) != EOF){if (ch >= 'A' && ch <= 'Z'){printf("%c\n", ch + 32);}else if (ch >= 'a' && ch <= 'z'){printf("%c\n",ch - 32);}}return 0;
}

当然,还有其他的写法,这里可以用到我们的库函数——islower,是专门用来判断小写字母的。

toupper——将小写字母转换为大写字母

tolower——将大写字母转换为小写字母

isupper——专门用来判断大写字母

 

 

 

 

 

#include
#include
int main()
{int ch = 0;//多组输入while ((ch = getchar()) != EOF){if (islower(ch)){printf("%c\n", toupper(ch));}else{printf("%c\n", tolower(ch));}//处理'\n'getchar();}return 0;
}

 

#include int main()
{int i=0;int j=0;while(scanf("%d %d",&i,&j)!=EOF){if(i>j){printf("%d>%d",i,j);}else if(i==j){printf("%d=%d",i,j);}else if(i

 

#includeint main()
{int t = 0;while (scanf("%d", &t) != EOF){if (t > 0){printf("%d\n", 1);}else if (t == 0){printf("%.1f\n", 0.5f);}else{printf("%d\n", 0);}}return 0;
}

#include 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&&a-b

 这里就是搞清楚三角形的判断规则,并去一一穷举就行了,因为只有三条边。

#include
int main()
{float weight = 0.0;float hight = 0.0;while (scanf("%f %f", &weight, &hight) != EOF){float bmi = weight / (hight * hight / 100 / 100);if (bmi < 18.5){printf("Underweight\n");}else if (bmi >= 18.5 && bmi <= 23.9){printf("Normal\n");}else if (bmi > 23.9 && bmi <= 27.9){printf("Overweight\n");}else{printf("Obese\n");}}return 0;
}

1. 多组输入

2. 浮点数除法要保证除号的两端至少有一个浮点数

 

 

#include 
#include int main()
{float a = 0.0;float b = 0.0;float c = 0.0;while (scanf("%f %f %f", &a, &b, &c) != EOF){if (a != 0){float disc = b * b - 4 * a * c;if (disc > 0.0){//有两个不相等的实根printf("x1=%.2f;x2=%.2f\n",(-b - sqrt(disc)) / (2 * a),(-b + sqrt(disc)) / (2 * a));}else if (disc < 0.0){//有两个虚根printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",(-b) / (2 * a), sqrt(-disc) / (2 * a),(-b) / (2 * a), sqrt(-disc) / (2 * a));}else{//有两个相等的实根printf("x1=x2=%.2f\n", (-b) / (2 * a));}}else{printf("Not quadratic equation\n");}}return 0;
}

1. 多组输入

2. 根据不同的限制条件给出相应的计算结果

3. 小数点数后位数有限制

 

#include int main()
{int y = 0;int m = 0;int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };while (scanf("%d%d", &y, &m) != EOF){int day = days[m - 1];if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){if (m == 2)day += 1;}printf("%d\n", day);}return 0;
}

1. 多组输入

2. 判断闰年,如果是闰年,2月多一天,其他都正常。

 

#include
#include
#include
void reverse(char* left, char* right)
{assert(left != NULL);assert(right != NULL);while (left < right){char* tmp = *left;*left = *right;*right = tmp;left++;right--;}
}
int main()
{char arr[100] = { 0 };gets(arr);//输入int len = strlen(arr);//求字符串的长度//1.逆序整个字符串reverse(arr, arr + len - 1);//2.逆序每个单词char* cur = arr;while (*cur){//找一个单词char* start = cur;while (*cur != ' ' && *cur != '\0'){cur++;//遇到空格,说明走到一个单词的最后面//遇到\0,说明已经走到字符串的最后面//这两种情况下都需要cur++}reverse(start, cur - 1);if (*cur == ' '){cur++;//跳过空格}}printf("%s\n", arr);return 0;
}

好啦,小雅兰今天的复习内容就到这里啦,以后的内容必定更加精彩!!!

 

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...