失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 黑马程序员------黑马基础测试题参考

黑马程序员------黑马基础测试题参考

时间:2021-04-04 16:05:42

相关推荐

黑马程序员------黑马基础测试题参考

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

黑马基础测试题抽中了几道比较复杂的题目,,已测试完美运行。

因为是零基础所以写的不好请见谅。如果同时零基础而且没思路的可以参考下,已加注释,还有勿抄。

题1:从键盘输入一大堆字符串,统计A、B、C、D的出现次数,最后出现次数由高到低输出字母和出现次数。(C语言)

<span style="font-size:14px;">#include <stdio.h>#include <string.h>int main(int argc, const char * argv[]) {printf("请输入一串字符串:\n"); //检测输入字符串char a[100];char *p= a;scanf("%s",p);unsigned long size =strlen(p);int count[4]={0,0,0,0}; //计数器初始化for (int i=0; i<size; i++) {if (a[i]=='A'||a[i]=='a') {count[0]++;}else if(a[i]=='B'||a[i]=='b'){count[1]++;}else if(a[i]=='C'||a[i]=='c'){count[2]++;}else if(a[i]=='D'||a[i]=='d'){count[3]++;}}struct test { //创建一个结构体int n;char c;};struct test array[4]={{count[0], 'A'},{count[1], 'B'},{count[2], 'C'},{count[3], 'D'},};for (int i=0;i<3;i++){ //循环比较两个数据大小,并按大小排列for (int j = 0; j<3-i; j++) {if (array[j].n<array[j+1].n) {struct test a = array[j]; //小的往后移一个array[j] = array[j+1];array[j+1]=a;}}}for (int i=0; i<4; i++) { //循环遍历,并输出分别个数printf("%c出现了%d次\n",array[i].c,array[i].n);}return 0;}</span>

题2:小明从1月1日开始,每三天结识一个美女两天结识一个帅哥,编程实现当输入1月1日之后的任意一天,输出小明那天是结识美女还是帅哥(注意润年问题)(C语言)

这道题其实就是算个总天数,很简单。但是要注意每个月有的天数不同,还有闰年问题。

<span style="font-size:14px;">#include <stdio.h>int total(int year1, int month1, int day1);int main(int argc, const char * argv[]) {int year =0;int month = 0;int day =0;printf("请按/1/1的格式输入一个日期: \n");while (year<||month<1||month>12||day<1||day>31) {printf("(日期需为1月1日之后的任意一天)\n");scanf("%d/%d/%d", &year, &month, &day);}int n = total(year, month, day);if (n%3==0) {printf("小明认识了一个美女\n");}if (n%2==0) {printf("小明认识了一个帅哥\n");}if (n%2!=0||n%3!=0) {printf("小明一个也没认识\n");}printf("总天数为: %d\n",n);return 0;}int total(int year1,int month1, int day1){int totaldays=0;int nyear=year1 -; //相差的年数int nmonth=month1 -1; //相差的月数int nday=day1 -1; //相差的日数int runnian=(nyear+2)/4;//计算润年数totaldays = 365*nyear +runnian; //按年数算要累加的天数if (nmonth==1) { //按不同月数累加间隔月的天数totaldays +=31;}else if (nmonth==2){totaldays +=31+28;}else if (nmonth==3){totaldays +=31+28+31;}else if (nmonth==4){totaldays +=31+28+31+30;}else if (nmonth==5){totaldays +=31+28+31+30+31;}else if (nmonth==6){totaldays +=31+28+31+30+31+30;}else if (nmonth==7){totaldays +=31+28+31+30+31+30+31;}else if (nmonth==8){totaldays +=31+28+31+30+31+30+31+31;}else if (nmonth==9){totaldays +=31+28+31+30+31+30+31+31+30;}else if (nmonth==10){totaldays +=31+28+31+30+31+30+31+31+30+31;}else if (nmonth==11){totaldays +=31+28+31+30+31+30+31+31+30+31+30;};totaldays += nday;//累加剩下差的天数if ((nmonth ==0||nmonth ==1)&&runnian!=0){totaldays =totaldays-1;//如果是1月或2月则不要累加上本闰年多的那一天}return totaldays;}</span>

如果觉得《黑马程序员------黑马基础测试题参考》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。