华为认证 百分网手机站

华为机试题及答案

时间:2017-09-28 15:45:17 华为认证 我要投稿

2016年华为机试题及答案

  1. 删除字符串中所有给定的子串(40分)

  问题描述:

  在给定字符串中查找所有特定子串并删除,如果没有找到相应子串,则不作任何操作。

  要求实现函数:

  int _sub_str(const char *str, const char *sub_str, char *result_str)

  【输入】 str:输入的操作字符串

  sub_str:需要查找并删除的特定子字符串

  【输出】 result_str:在str字符串中删除所有sub_str子字符串后的结果

  【返回】 删除的子字符串的个数

  注:

  I、 子串匹配只考虑最左匹配情况,即只需要从左到右进行字串匹配的情况。比如:

  在字符串"abababab"中,采用最左匹配子串"aba",可以匹配2个"aba"字串。如果

  匹配出从左到右位置2开始的"aba",则不是最左匹配,且只能匹配出1个"aba"字串。

  II、 输入字符串不会超过100 Bytes,请不用考虑超长字符串的情况。

  示例

  输入:str = "abcde123abcd123"

  sub_str = "123"

  输出:result_str = "abcdeabcd"

  返回:2

  输入:str = "abcde123abcd123"

  sub_str = "1234"

  输出:result_str = "abcde123abcd123"

  返回:0

  [cpp] view plain copy#include

  #include

  int _sub_str(const char *str, const char *sub_str, char *result_str)

  {

  int i,j,total=0;

  int len_str = strlen(str);

  int len_sub_str = strlen(sub_str);

  char temp[100]={0};

  while(*str)

  {

  memcpy(temp,str,len_sub_str);

  if(strcmp(sub_str,temp)!=0)

  *result_str++ = *str++;

  else

  {

  str+=len_sub_str;

  total++;

  }

  }

  *result_str = '';

  return total;

  }

  int main()

  {

  const char str[100]="abcde123abcd123";

  const char sub_str[100]="123";

  char result_str[100]={0};

  int total=_sub_str(str, sub_str, result_str);

  printf("result is %s ",result_str);

  printf("the num is %d ",total);

  system("pause");

  return 0;

  }

  手机号码合法性判断(20分)

  问题描述:

  我国大陆运营商的手机号码标准格式为:国家码+手机号码,例如:8613912345678。特点如下:

  1、 长度13位;

  2、 以86的国家码打头;

  3、 手机号码的每一位都是数字。

  请实现手机号码合法性判断的函数(注:考生无需关注手机号码的真实性,也就是说诸如86123123456789这样的`手机号码,我们也认为是合法的),要求:

  1) 如果手机号码合法,返回0;

  2) 如果手机号码长度不合法,返回1

  3) 如果手机号码中包含非数字的字符,返回2;

  4) 如果手机号码不是以86打头的,返回3;

  【注】除成功的情况外,以上其他合法性判断的优先级依次降低。也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断。

  要求实现函数:

  int verifyMsisdn(char* inMsisdn)

  【输入】 char* inMsisdn,表示输入的手机号码字符串。

  【输出】 无

  【返回】 判断的结果,类型为int。

  示例

  输入: inMsisdn =“869123456789“

  输出: 无

  返回: 1

  输入: inMsisdn =“88139123456789“

  输出: 无

  返回: 3

  输入: inMsisdn =“86139123456789“

  输出: 无

  返回: 0

  [cpp] view plain copy#include

  #include

  int verifyMsisdn(char* inMsisdn)

  {

  int i,flag1 = 0,flag2 = 0,flag3 = 1;

  if(strlen(inMsisdn) == 13)

  flag1 = 1;

  if(inMsisdn[0] == '8' && inMsisdn[1] == '6')

  flag2 = 1;

  for(i=0 ; i< strlen(inMsisdn);i++)

  if(!(inMsisdn[i]>='0' && inMsisdn[i]<= '9'))

  flag3 = 0;

  if(flag1 && flag2 && flag3)

  return 0;

  else if(!flag1)

  return 1;

  else if(!flag3)

  return 2;

  else if(!flag2)

  return 3;

  }

  int main()

  {

  char inMsisdn[20]={0};

  int return_num;

  printf("please input the mobile num: ");

  scanf("%s",inMsisdn);

  return_num = verifyMsisdn(inMsisdn);

  printf("the return num is:%d",return_num);

  system("pause");

  return 0;

  }

  将一个字符串的元音字母复制到另一个字符串,并排序(30分)

  问题描述:

  有一字符串,里面可能包含英文字母(大写、小写)、数字、特殊字符,现在需要实现一函数,将此字符串中的元音字母挑选出来,存入另一个字符串中,并对字符串中的字母进行从小到大的排序(小写的元音字母在前,大写的元音字母在后,依次有序)。

  说明:

  1、 元音字母是a,e,i,o,u,A,E,I,O,U。

  2、 筛选出来的元音字母,不需要剔重;

  最终输出的字符串,小写元音字母排在前面,大写元音字母排在后面,依次有序。

  要求实现函数:

  void sortVowel (char* input, char* output);

  【输入】 char* input,表示输入的字符串

  【输出】 char* output,排好序之后的元音字符串。

  【返回】 无

  示例

  输入:char *input =“Abort!May Be Some Errors In Out System.“

  输出:char *output =“aeeeooAEIO“

  [cpp] view plain copy#include

  #include

  void sort(char * arr,int n)

  {

  int i,j;

  char tem;

  for(i=0 ;i

  {

  for(j=i+1 ; j

  if(arr[i] > arr[j])

  {

  tem = arr[i];

  arr[i] = arr[j];

  arr[j] = tem;

  }

  }

  }

  void sortVowel (char* input, char* output)

  {

  char lit_ch[5] ={'a','e','i','o','u'};

  char big_ch[5] ={'A','E','I','O','U'};

  char lit_temp[100]={0},big_temp[100]={0};

  int i;

  char *p_lit = lit_temp,*p_big = big_temp;

  while(*input)

  {

  for(i=0 ; i<5 ;i++)

  {

  if(*input == lit_ch[i])

  *p_lit++ = *input;

  if(*input == big_ch[i])

  *p_big++ = *input;

  }

  input++;

  }

  *p_lit = '';

  *p_big = '';

  /*printf("lit_temp before sort is :%s ",lit_temp);

  printf("big_temp before sort is :%s ",big_temp);*/

  sort(lit_temp,strlen(lit_temp));

  sort(big_temp,strlen(big_temp));

  /*printf("lit_temp after sort is :%s ",lit_temp);

  printf("big_temp after sort is :%s ",big_temp);*/

  strcat(lit_temp,big_temp);

  /*printf(" after strcat is :%s ",lit_temp);*/

  strcpy(output,lit_temp);

  }

  int main()

  {

  char input[100],output[100];

  printf("please input: ");

  //scanf("%s",input);

  gets(input);

  printf("input is :%s ",input);

  sortVowel(input,output);

  printf("the processed is:%s ",output);

  system("pause");

  return 0;

  }

  请实现身份证号码合法性判断的函数。除满足以上要求外,需要对持有人生日的年、月、日信息进行校验。年份大于等于1900年,小于等于2100年。需要考虑闰年、大小月的情况。所谓闰年,能被4整除且不能被100整除或能被400整除的年份,闰年的2月份为29天,非闰年的2月份为28天。其他情况的合法性校验,考生不用考虑。

  函数返回值:

  1) 如果身份证号合法,返回0;

  2) 如果身份证号长度不合法,返回1;

  3) 如果身份证号第1~17位含有非数字的字符,返回2;

  4) 如果身份证号第18位既不是数字也不是英文小写字母x,返回3;

  5) 如果身份证号的年信息非法,返回4;

  6) 如果身份证号的月信息非法,返回5;

  7) 如果身份证号的日信息非法,返回6(请注意闰年的情况);

  【注】除成功的情况外,以上其他合法性判断的优先级依次降低。也就是说,如果判断出长度不合法,直接返回1即可,不需要再做其他合法性判断。