2015年华为校招机试题和代码实现(分解字符串,拼音转数字,去除

再来一套2015年的华为机试题。

第一题(60分):

按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000” 分析思路: 容易题 1.获得字符串的长度length后,判断与要输出位数N的大小,大于N的话,直接printf前N位字符,然后length-=N; 2.再次循环判断与要输出位数N的大小,如果小于N的话,输出length个字符和n-length个0字符。 3.重复循环直到length<0。 代码:

#include <iostream>using namespace std;/*********************************2015届华为校招机试题——分解字符串)Author:牧之丶 Date:2015年5月25日Email:bzhou84@163.com**********************************/void resolve(char *str,int N){int length=strlen(str);//获得字符串的长度lengthchar strtemp[255];while(length>0){//循环直到length<0if (length>=N)//大于N,直接printf前N位字符{int i;for (i=0;i<N;i++){strtemp[i]=*str;str++;}strtemp[i]=’\0′;////记得添加\0 }else//小于N,输出length个字符和n-length个0字符{int i;for (i=0;i<length;i++){strtemp[i]=*str;str++;}for (i=length;i<N;i++){strtemp[i]=’0′;}strtemp[i]=’\0′;}cout<<strtemp<<” “;length-=N;}}int main(){int M,N;char str[255];while (cin>>M>>N){for (int i=0;i<M;i++){scanf(“%s”,str);resolve(str,N);}}return 0;}第一题 拼音转数字

输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下: 描述: 拼音 yi er san si wu liu qi ba jiu 阿拉伯数字 1 2 3 4 5 6 7 8 9 输入字符只包含小写字母,,所有字符都可以正好匹配

运行时间限制:无限制 内存限制: 无限制 输入: 一行字符串,长度小于1000 输出: 一行字符(数字)串 样例输入: yiersansi 样例输出: 1234 **思路:简单题,判断首字符即可。 代码:**

/*********************************2015届华为校招机试题——拼音转数字)Author:牧之丶 Date:2015年5月25日Email:bzhou84@163.com**********************************/;void convert(char *str){int i;int length;for(i = 0 ; i < length ; ){switch(str[i]){case ‘y’:cout<<“1”;i += 2;break;case ‘e’:cout<<“2”;i += 2;break;case ‘s’:if(str[i + 1] == ‘a’){cout<<“3”;i += 3;}else{cout<<“4”;i += 2;}break;case ‘w’:cout<<“5”;i += 2;break;case ‘l’:cout<<“6”;i += 3;break;case ‘q’:cout<<“7”;i += 2;break;case ‘b’:cout<<“8”;i += 2;break;case ‘j’:cout<<“9”;i += 3;break;}}printf(“\n”);}int main(){char str[255];while(cin>>str){convert(str);}return 0;}第二题:去除重复字符并排序

运行时间限制:无限制 内容限制:无限制 输入:字符串 输出:去除重复字符并排序的字符串 样例输入:aabcdefff 样例输出:abcdef 分析思路: 一、用map实现,map的特征:键不允许重复,比较函数只对元素的键值进行比较,小到大排序 代码:

/*********************************2015届华为校招机试题——去除重复字符并排序Author:牧之丶 Date:2015年5月25日Email:bzhou84@163.com**********************************/;void fun(char *str){map<char,int> m;int length=strlen(str);for (int i=0;i<length;i++){m[str[i]]+=1;}map<char,int>::iterator it;for (it=m.begin();it!=m.end();it++){cout<<(*it).first;}cout<<endl;}int main(){char str[255];while(cin>>str){fun(str);}return 0;}听他第二十八次提起童年往事,每年的同一天和他庆祝生日,

2015年华为校招机试题和代码实现(分解字符串,拼音转数字,去除

相关文章:

你感兴趣的文章:

标签云: