前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2015届华为校园招聘机试题

2015届华为校园招聘机试题

作者头像
bear_fish
发布2018-09-20 16:17:20
4900
发布2018-09-20 16:17:20
举报

第一题(60分):        按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”

[cpp] view plaincopy

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767
  2. #include<iostream>
  3. #include<cstdio>
  4. using namespace std;  
  5. void solve(char *str , int n , int len)  
  6. {  
  7. int i , j , k , quotient , remainder;  
  8.     quotient = len / n;                //原字符串被分解的个数
  9.     remainder = len - n * quotient;    //剩余的字符串的个数
  10. for(i = 0 ; i < len ; i += n)  
  11.     {  
  12. if(len - i < n)  
  13.         {  
  14.              k = n - len + i;  
  15. for(j = i ; j < len ; ++j)  
  16.                  printf("%c" , str[j]);  
  17. for(j = 0 ; j < k ; ++j)  
  18.                  putchar('0');  
  19.         }  
  20. else
  21.         {  
  22. for(j = i ; j < i + n ; ++j)  
  23.                 printf("%c" , str[j]);  
  24.         }  
  25.         putchar(' ');  
  26.     }  
  27.     printf("\n");  
  28. }  
  29. int main(void)  
  30. {  
  31. int i , m , n , len;  
  32. char str[1000];  
  33. while(scanf("%d %d", &m , &n) != EOF)  
  34.     {  
  35. for(i = 0 ; i < m ; ++i)  
  36.         {  
  37.             scanf("%s" , str);  
  38.             len = strlen(str);  
  39.             solve(str , n , len);  
  40.         }  
  41.     }  
  42. return 0;  
  43. }  

第一题:拼音转数字 输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下: 描述:      拼音        yi  er  san  si  wu  liu  qi  ba  jiu       阿拉伯数字        1   2   3      4   5    6    7   8   9 输入字符只包含小写字母,所有字符都可以正好匹配 运行时间限制:无限制 内存限制:       无限制 输入:              一行字符串,长度小于1000 输出:              一行字符(数字)串 样例输入:       yiersansi 样例输出:       1234

[cpp] view plaincopy

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767
  2. #include<iostream>
  3. #include<cstdio>
  4. using namespace std;  
  5. void solve(char *str , int len)  
  6. {  
  7. int i;  
  8. for(i = 0 ; i < len ; )  
  9.     {  
  10. switch(str[i])  
  11.         {  
  12. case 'y':  
  13.             putchar('1');  
  14.             i += 2;  
  15. break;  
  16. case 'e':  
  17.             putchar('2');  
  18.             i += 2;  
  19. break;  
  20. case 's':  
  21. if(str[i + 1] == 'a')  
  22.             {  
  23.                 putchar('3');  
  24.                 i += 3;  
  25.             }  
  26. else
  27.             {  
  28.                 putchar('4');  
  29.                 i += 2;  
  30.             }  
  31. break;  
  32. case 'w':  
  33.             putchar('5');  
  34.             i += 2;  
  35. break;  
  36. case 'l':  
  37.             putchar('6');  
  38.             i += 3;  
  39. break;  
  40. case 'q':  
  41.             putchar('7');  
  42.             i += 2;  
  43. break;  
  44. case 'b':  
  45.             putchar('8');  
  46.             i += 2;  
  47. break;  
  48. case 'j':  
  49.             putchar('9');  
  50.             i += 3;  
  51. break;  
  52.         }  
  53.     }  
  54.     printf("\n");  
  55. }  
  56. int main(void)  
  57. {  
  58. int len;  
  59. char str[1000];  
  60. while(scanf("%s" , str) != EOF)  
  61.     {  
  62.         len = strlen(str);  
  63.         solve(str , len);  
  64.     }  
  65. return 0;  
  66. }  

第二题:去除重复字符并排序 运行时间限制:无限制 内容限制:       无限制 输入:              字符串 输出:              去除重复字符并排序的字符串 样例输入:       aabcdefff 样例输出:       abcdef

[cpp] view plaincopy

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<memory>
  5. using namespace std;  
  6. void solve(char *str , int len)  
  7. {  
  8. int i , hash[256];  
  9.     memset(hash , 0 , sizeof(hash));  
  10. for(i = 0 ; i < len ; ++i)  
  11.     {  
  12. if(0 == hash[str[i]])  
  13.             hash[str[i]] = 1;  
  14.     }  
  15. for(i = 0 ; i < 256 ; ++i)  
  16.     {  
  17. if(0 != hash[i])  
  18.             putchar(i);  
  19.     }  
  20.     printf("\n");  
  21. }  
  22. int main(void)  
  23. {  
  24. int len;  
  25. char str[1000];  
  26. while(scanf("%s" , str) != EOF)  
  27.     {  
  28.         len = strlen(str);  
  29.         solve(str , len);  
  30.     }  
  31. return 0;  
  32. }  

第三题:等式变换 输入一个正整数X,在下面的等式左边的数字之间添加+号或者-号,使得等式成立。 1 2 3 4 5 6 7 8 9 = X 比如: 12-34+5-67+89 = 5 1+23+4-5+6-7-8-9 = 5 请编写程序,统计满足输入整数的所有整数个数。 输入:       正整数,等式右边的数字 输出:       使该等式成立的个数 样例输入:5 样例输出:21

[cpp] view plaincopy

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767
  2. #include<iostream>
  3. #include<cstdio>
  4. using namespace std;  
  5. int ops[21];  
  6. const char sym[3] = {'+' , '-' , ' '};  
  7. int result , num;  
  8. void dfs(int layer, int currentResult, int lastOp, int lastSum)  
  9. {  
  10.     lastSum *= (layer > 9) ? 100 : 10;  
  11.     lastSum += layer;  
  12. if(layer == 9)  
  13.     {  
  14.         currentResult += (lastOp) ? (-1 * lastSum) : lastSum;  
  15. if(currentResult == result)  
  16.         {  
  17.             ++num;  
  18.             printf("1");  
  19. for(int i = 2 ; i <= 9 ; ++i)  
  20.             {  
  21. if(sym[ops[i-1]] != ' ')  
  22.                     printf(" %c ", sym[ops[i-1]]);  
  23.                 printf("%d", i);  
  24.             }  
  25.             printf(" = %d\n" , result);  
  26.         }  
  27. return;  
  28.     }  
  29.     ops[layer] = 2;  
  30.     dfs(layer + 1 , currentResult , lastOp , lastSum);   //Continue
  31.     currentResult += (lastOp)? (-1 * lastSum) : lastSum;  
  32.     ops[layer] = 0;  
  33.     dfs(layer + 1 , currentResult , 0 , 0);  //Plus
  34.     ops[layer] = 1;  
  35.     dfs(layer + 1 , currentResult , 1 , 0);  //Minus
  36. }  
  37. int main(void)  
  38. {  
  39. while(scanf("%d", &result) != EOF)  
  40.     {  
  41.         num = 0;  
  42.         dfs(1 , 0 , 0 , 0);  
  43.         printf("%d\n" , num);  
  44.     }  
  45. return 0;  
  46. }  

转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年01月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档