您的位置:首页 > 职场人生

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

2015-02-10 19:50 274 查看
------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:小明从2006年1月1日开始,每三天结识一个美女两天结识一个帅哥,编程实现当输入2006年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("请按2006/1/1的格式输入一个日期: \n");
while (year<2006||month<1||month>12||day<1||day>31) {
printf("(日期需为2006年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 -2006; //相差的年数
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: