當前位置:學者齋 >

IT認證 >華為認證 >

2016年華為機試題及答案

2016年華為機試題及答案

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

2016年華為機試題及答案

問題描述:

在給定字符串中查找所有特定子串並刪除,如果沒有找到相應子串,則不作任何操作。

要求實現函數:

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即可,不需要再做其他合法性判斷。

標籤: 華為 試題
  • 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-hk/itrz/huawei/wv2n35.html