华为2014笔试算法题汇总.docVIP

  • 9
  • 0
  • 约6.34千字
  • 约 13页
  • 2016-08-28 发布于河南
  • 举报
华为2014笔试算法题汇总

华为2014笔试算法题汇总[cpp] view plaincopy ////////////////////////////////////////////////////////////////////////// #include?iostream???? #include?cassert???? ???? using?namespace?std;???? ???? bool?g_flag[26];???? void?stringFilter(const?char?*pInputStr,?long?lInputLen,?char?*pOutputStr)???? {???? 牋?assert(pInputStr?!=?NULL);???? 牋sint?i?=?0;???? 牋nif?(pInputStr?==?NULL?||?lInputLen?=?1)???? 牋f{???? 牋牋牋?? return;???? 牋e}???? 牋?const?char?*p?=?pInputStr;???? 牋owhile(*p?!=?\0)???? 牋h{???? 牋牋???if?(g_flag[(*p?-?a)])???? 牋牋?(g{???? 牋牋牋牋? lap++;???? 牋牋?;?}else{???? 牋牋牋牋?{???pOutputStr[i++]?=?*p;???? 牋牋牋牋?utStg_flag[*p?-?a]?=?1;???? 牋牋牋牋?g[*pp++;???? 牋牋?;?}???? 牋?}???? 牋?pOutputStr[i]?=?\0;???? }???? int?main()???? {???? 牋牋utmemset(g_flag,0,sizeof(g_flag));???? ????char?input[]?=?abacacde;???? ????char?*output?=?new?char[strlen(input)?+?1];???? ????stringFilter(input,strlen(input),output);???? ????coutoutputendl;???? ????delete?output;???? ????return?0;???? }???? 2. 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。 压缩规则: 1、仅压缩连续重复出现的字符。比如字符串abcbc由于无连续重复字符,压缩后的字符串还是abcbc。 2、压缩字段的格式为字符重复的次数+字符。例如:字符串xxxyyyyyyz压缩后就成为3x6yz。 要求实现函数:? void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr); 【输入】 pInputStr:? 输入字符串 ??????????? lInputLen:? 输入字符串长度 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 示例? 输入:“cccddecc”?? 输出:“3c2de2c” 输入:“adef”???? 输出:“adef” 输入:“pppppppp” 输出:“8p” [cpp] view plaincopy //////////////////////////////////////////////////////////////////////////牋?字符#include?iostream???? #include?cassert???? 牋牋nc using?namespace?std;???? 牋牋us void?stringZip(const?char?*pInputStr,?long?lInputLen,?char?*pOutputStr)???? {???? 牋 const?char?*p?=?pInputStr;???? 牋oint?num?=?1;???? 牋nint?i?=?0;???? 牋np++;???? 牋+while(*p?!=?NULL)???? 牋h{???? 牋牋牋?? while(*p?==?*(p-1)?*p?!=?NULL)???? 牋牋牋le({???? 牋牋牋?? num++;???? 牋牋牋?+;?p++;???? 牋牋牋;??}???? 牋牋牋?? if?(num??1)???? 牋牋牋(nu{???? 牋牋牋牋牋? ??1int?size?=?0;???? 牋牋牋牋牋?ze?=?int?temp?=?num;???? 牋?

文档评论(0)

1亿VIP精品文档

相关文档