系列文章目录
身躯已然冰封,灵魂仍旧火热
本文由@睡觉待开机原创,未经允许不得转载。
本内容在csdn网站首发
欢迎各位点赞—评论—收藏
如果存在不足之处请评论留言,共同进步!
文章目录
- 系列文章目录
- 前言
- 题目链接(有需要请自行链接做题)
- T1:
- 题目解析:
- 思路1:
- 思路2:
- 思路3:
- T2:
- 思路1:
- 思路2:
- T3:
- 思路1:
- T4:
- 思路1:
- T5:
- 思路1:
- T6:
- 思路1:
- 思路2:
- T7:
- 思路1:
- 思路2:
- 思路3:
- T8:
- 思路1:
- 思路2:
- T9:
- 思路1:
- 思路2:
- 思路3:
- 所有代码展示:
- 总结:
前言
注:本文题目是作者听课视频讲解,来自比特鹏哥C语言刷题训练营,有些许代码思路借鉴。
在本节博客当中,题目难度有了一点点提升,然后就是更注重一些题目的多解性了,仔细对比不同的代码和思路解法,就能促进自己对代码的理解和掌握。
题目链接(有需要请自行链接做题)
第5讲
- 精选社⻓ 题号:BC147 链接:https://www.nowcoder.com/practic
e/45a30e3ef51040ed8a7674984d6d1553?tpId=290&tqId=39935&ru=/exam/oj - 你是天才吗 题号:BC50 链接:https://www.nowcoder.com/practic
e/557cc54704bb4d56b73b62d1a5455331?tpId=290&tqId=39838&ru=/exam/oj - 完美成绩 题号:(⽆) 链接:https://www.nowcoder.com/questionTe
rminal/8312e497509a450f968d9a6a2381ce32 - 及格分数 题号:BC51 链接:https://www.nowcoder.com/practice/
56513524333148b38945e1989bc7df4e?tpId=290&tqId=39839&ru=/exam/oj - 判断整数的奇偶性 题号:BC52 链接:https://www.nowcoder.com/
practice/a8b018667e274af29b5f76a6673450fc?tpId=290&tqId=39840&ru=/exam/oj - 最⾼分数 题号:(⽆) 链接:https://www.nowcoder.com/questio
nTerminal/52c18a3b49a54fc98107fbdde1415f90 - 判断元⾳还是辅⾳ 40题号:BC53 链接:https://www.nowcoder.com/p
ractice/7eb4df4d52c44d309081509cf52ecbc4?tpId=290&tqId=39841&ru=/exam/oj - 判断是不是字⺟ 题号:BC60 链接:https://www.nowcoder.com/practice/91a588dd4cd244bfa616f17603ec123c?tpId=290&tqId=39848&ru=/exam/oj
- 大小写字母转换 题号:BC48 链接:https://www.nowcoder.com/practice/850ebd30a2a34cfc87199da3fc15786a?tpId=107&&tqId=33329&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking
T1:
题目解析:
看着挺高大上,还是竞选社长,其实说白了就是给一串字符串,然后这个字符串里有A有B,该字符串以0为结束标志,让你数一数哪个多罢了。
思路1:
创建一个数组,存储一下字符串,然后用数组+循环进行挨个访问,挨个计数就行了
代码实现:
void T1_way1(void)
{//创建一个数组,读入并存储字符串char str[100] = { 0 };scanf("%s", str);//一般scanf需要给一个地址,这里数组名就是数组首元素地址//循环依次访问,直到遇到0或者EOFint i = 0;int count_a = 0;int count_b = 0;while (str[i] != '0' && str[i] != EOF){if (str[i] == 'A')count_a++;if (str[i] == 'B')count_b++;i++;}//按照情况来打印if (count_a > count_b)printf("A\n");else if (count_a < count_b)printf("B\n");elseprintf("E\n");
}
思路2:
其实计数这个地方可以升级一下哈,咱有俩计数的变量,有点多,其实一个就够了,数个A计数+1,数个B-计数就1,这样的话是比较不错的。
然后代码就变成这样了:
void T1_way2(void)
{//创建一个数组,读入并存储字符串char str[100] = { 0 };scanf("%s", str);//一般scanf需要给一个地址,这里数组名就是数组首元素地址//循环依次访问,直到遇到0或者EOFint i = 0;int count = 0;while (str[i] != '0' && str[i] != EOF){if (str[i] == 'A')count++;if (str[i] == 'B')count--;i++;}//按照情况来打印if (count > 0)printf("A\n");else if (count < 0)printf("B\n");elseprintf("E\n");
}
思路3:
感觉我们得把一个字符串需要存起来,存起来的话,是不是有点浪费空间,我们到最后又不需要这个数组里的值,,,只是看看A多还是B多而已。。。要不我干脆补存储字符串了,一边读,一边计数不就得了
void T1_way3(void)
{//一边读一边计数,循环依次访问,直到遇到0或者EOFchar ch = '0';int count = 0;while (ch = getchar() != '0'&& ch != EOF){if (ch == 'A')count++;if (ch == 'B')count--;}//按照情况来打印if (count > 0)printf("A\n");else if (count < 0)printf("B\n");elseprintf("E\n");
}
这样的话其实没了数组计数变量也被我们搞成一个了,所以说应该是代码效率提升了不少哈哈,所以这种简单题目可以试试能不能我们把他的这个效率还有需要的内存给他搞得好一点。。。注意细节得。
T2:
题意:这个题也是一个超级简单的题目,说白了就是编一个程序,这个程序是这样滴:搞一个比较值,一个数字大于等于这个比较值,就是天才,小于就不是。
思路1:
直接搞俩变量,一个是存储这个比较值另一个输入值就行了
void T2_way1(void)
{int a = 140;int iq = 0;while(scanf("%d", &iq)!=EOF){if (iq >= a)printf("Genius\n");}
}
思路2:
其实这个多组输入还可以换一种写法:
void T2_way2(void)
{int a = 140;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Genius\n");}
}
可以自己稍微考量一下为啥这个多组输入还可以这么写哈,不会的私信我,我给你说哈。
然后这个题目就没啥好说的了吧,毕竟太简单了,没有可以改进的地方啊。。。
T3:
额,其实这个题目跟上一个一样的,就是改了改那个比较数字而已。。。
思路1:
代码我就直接copy上一个的代码了,没事可以自己再试一下。。。
void T3_way1(void)
{int a = 90;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Perfect\n");}
}
T4:
题目解析:跟前面俩题一样的,不多废话了。。。感觉这三个题目选的有点划水的感觉。。。
思路1:
void T4_way1(void)
{int a = 60;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Pass\n");elseprintf("Fail\n");}
}
T5:
题目解析:
就是你搞个数字进去,让程序看看是偶数还是奇数。。。用%操作就行了。
思路1:
void T5_way1(void)
{int num = 0;while (scanf("%d", &num) != EOF){if (num % 2 == 0)printf("Even\n");elseprintf("Odd\n");}
}
T6:
题目解析:就是输入三个数字,程序输出其中的最大的数字。
额。。。这个题目的话很多人可能想到了类似于打擂台的那种方式。。。其实这种三个比较量的时候,可以用三目操作符搞一下。
思路1:
三目操作符的使用,主要就是复习一下,省的忘干净C语言还有这个东西,这东西用起来其实相当好用的,不过很多人想不到而已。
void T6_way1(void)
{int a, b, c;while (~scanf("%d %d %d", &a, &b, &c)){int max = a > b ? a : b;max = max > c ? max : c;printf("%d\n", max);}}
思路2:
就是平常那种打擂台的方式,或者再加上数组高级一点。
void T6_way2(void)
{int n1 = 0;int n2 = 0;int n3 = 0;int max = 0;while (scanf("%d %d %d",&n1,&n2,&n3) != EOF){max = n1;if (n2 > max)max = n2;if (n3 > max)max = n3;printf("%d\n", max);}}
T7:
题目分析:就是输入一个字母看看是元音还是辅音。。。
思路1:
void T7_way1(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf("%c", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == str[i]){printf("Vowel\n");break;}}getchar();if (i == 10)printf("Consonant\n");}
}
这里得说一下哈,scanf输入字符时候,我们按回车也算一个字符,为了把那个回车给“吃掉”,所以就有搞了个getchar()函数。。。
思路2:
然后那个回车问题的话,其实在scanf%c前面加个空格也可以解决哈,就是也可以不用特地加一个getchar()来“吃掉”多余的回车。。。
void T7_way2(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf(" %c", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == str[i]){printf("Vowel\n");break;}}if (i == 10)printf("Consonant\n");}
}
思路3:
void T7_way3(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf(" %c", &ch) != EOF){if (strchr(str,ch)){printf("Vowel\n");}elseprintf("Consonant\n");}
}
然后这里简单提及一下strchr函数吧,就是一个库函数,这个库函数是用来判断一个字符是否在某个数组当中。。。有的话就返回非0,没有的话返回0。。。
T8:
思路1:
直接用库函数判断
头文件:#include<string.h>
void T8_way1(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (isalpha(ch))printf("%c is an alphabet.\n",ch);elseprintf("%c is not an alphabet.\n",ch);}
}
思路2:
oid T8_way2(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (('a'<= ch && ch <= 'z') ||('A' <= ch && ch <= 'Z'))printf("%c is an alphabet.\n",ch);elseprintf("%c is not an alphabet.\n",ch);}
}
T9:
思路1:
void T9_way1(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if ('a' <= ch && ch <= 'z')printf("%c", ch-32);else if('A' <= ch && ch <= 'Z')printf("%c\n", ch+32);}
}
思路2:
利用库函数
islower,isupper
头文件:#include<ctype.h>
void T9_way2(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (islower(ch))printf("%c", ch - 32);else if (isupper(ch))printf("%c\n", ch + 32);}
}
思路3:
还有库函数tolower,toupper
void T9_way3(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (islower(ch))printf("%c", toupper(ch));else if (isupper(ch))printf("%c\n", tolower(ch));}
}
所有代码展示:
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<ctype.h>void T1_way1(void)
{//创建一个数组,读入并存储字符串char str[100] = { 0 };scanf("%s", str);//一般scanf需要给一个地址,这里数组名就是数组首元素地址//循环依次访问,直到遇到0或者EOFint i = 0;int count_a = 0;int count_b = 0;while (str[i] != '0' && str[i] != EOF){if (str[i] == 'A')count_a++;if (str[i] == 'B')count_b++;i++;}//按照情况来打印if (count_a > count_b)printf("A\n");else if (count_a < count_b)printf("B\n");elseprintf("E\n");
}void T1_way2(void)
{//创建一个数组,读入并存储字符串char str[100] = { 0 };scanf("%s", str);//一般scanf需要给一个地址,这里数组名就是数组首元素地址//循环依次访问,直到遇到0或者EOFint i = 0;int count = 0;while (str[i] != '0' && str[i] != EOF){if (str[i] == 'A')count++;if (str[i] == 'B')count--;i++;}//按照情况来打印if (count > 0)printf("A\n");else if (count < 0)printf("B\n");elseprintf("E\n");
}void T1_way3(void)
{//一边读一边计数,循环依次访问,直到遇到0或者EOFchar ch = '0';int count = 0;while (ch = getchar() != '0' && ch != EOF){if (ch == 'A')count++;if (ch == 'B')count--;}//按照情况来打印if (count > 0)printf("A\n");else if (count < 0)printf("B\n");elseprintf("E\n");
}void T2_way1(void)
{int a = 140;int iq = 0;while (scanf("%d", &iq) != EOF){if (iq >= a)printf("Genius\n");}
}void T2_way2(void)
{int a = 140;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Genius\n");}
}void T3_way1(void)
{int a = 90;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Perfect\n");}
}void T4_way1(void)
{int a = 60;int iq = 0;while (~scanf("%d", &iq)){if (iq >= a)printf("Pass\n");elseprintf("Fail\n");}
}void T5_way1(void)
{int num = 0;while (scanf("%d", &num) != EOF){if (num % 2 == 0)printf("Even\n");elseprintf("Odd\n");}
}void T6_way1(void)
{int a, b, c;while (~scanf("%d %d %d", &a, &b, &c)){int max = a > b ? a : b;max = max > c ? max : c;printf("%d\n", max);}}void T6_way2(void)
{int n1 = 0;int n2 = 0;int n3 = 0;int max = 0;while (scanf("%d %d %d", &n1, &n2, &n3) != EOF){max = n1;if (n2 > max)max = n2;if (n3 > max)max = n3;printf("%d\n", max);}}void T7_way1(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf("%c", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == str[i]){printf("Vowel\n");break;}}getchar();if (i == 10)printf("Consonant\n");}
}void T7_way2(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf(" %c", &ch) != EOF){int i = 0;for (i = 0; i < 10; i++){if (ch == str[i]){printf("Vowel\n");break;}}if (i == 10)printf("Consonant\n");}
}void T7_way3(void)
{char str[] = "AEIOUaeiou";char ch = 0;while (scanf(" %c", &ch) != EOF){if (strchr(str, ch)){printf("Vowel\n");}elseprintf("Consonant\n");}
}void test(void)
{char str[] = "AEIOUaeiou";char ch = 0;scanf("%c", &ch);int c = strchr(str, ch);printf("%d", c);
}void T8_way1(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (isalpha(ch))printf("%c is an alphabet.\n", ch);elseprintf("%c is not an alphabet.\n", ch);}
}void T8_way2(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'))printf("%c is an alphabet.\n", ch);elseprintf("%c is not an alphabet.\n", ch);}
}void T9_way1(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if ('a' <= ch && ch <= 'z')printf("%c", ch - 32);else if ('A' <= ch && ch <= 'Z')printf("%c\n", ch + 32);}
}void T9_way2(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (islower(ch))printf("%c", ch - 32);else if (isupper(ch))printf("%c\n", ch + 32);}
}void T9_way3(void)
{char ch = 0;while (scanf(" %c", &ch) != EOF){if (islower(ch))printf("%c", toupper(ch));else if (isupper(ch))printf("%c\n", tolower(ch));}
}int main()
{//T1_way1();//T1_way2();//T1_way3();//T2_way1();//T2_way2();//T3_way1();//T4_way1();//T5_way1();//T6_way1();//T6_way2();//T7_way1();//T7_way2();//test();//T8_way1();//T8_way2();//T9_way1();//T9_way2();T9_way3();return 0;
}
总结:
以上就是对鹏哥刷题训练营第五节课所有的代码笔记了~希望同学们还是自己动手敲一下。