当前位置:文档之家› 华为2015校园招聘上机考试题

华为2015校园招聘上机考试题

华为2015校园招聘上机考试题第一题(60分):按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。

例如:输入2,8,“abc”,“123456789”,则输出为“abc00000”,“12345678“,”90000000”1.转载请标明出处,原文地址:/hackbuteer1/article/details/392537672.#include<iostream>3.#include<cstdio>ing namespace std;5.6.void solve(char *str , int n , int len)7.{8.int i , j , k , quotient , remainder;9.quotient = len / n;//原字符串被分解的个数10. remainder = len - n * quotient; //剩余的字符串的个数11.12. for(i = 0 ; i < len ; i += n)13. {14. if(len - i < n)15. {16. k = n - len + i;17. for(j = i ; j < len ; ++j)18. printf("%c" , str[j]);19. for(j = 0 ; j < k ; ++j)20. putchar('0');21. }22. else23. {24. for(j = i ; j < i + n ; ++j)25. printf("%c" , str[j]);26. }27. putchar(' ');28. }29. printf("\n");30.}31.32.int main(void)33.{34. int i , m , n , len;35. char str[1000];36.37. while(scanf("%d %d", &m , &n) != EOF)38. {39. for(i = 0 ; i < m ; ++i)40. {41. scanf("%s" , str);42. len = strlen(str);43. solve(str , n , len);44. }45. }46. return 0;47.}第一题:拼音转数字输入是一个只包含拼音的字符串,请输出对应的数字序列。

转换关系如下:描述:拼音yier san siwuliu qi bajiu阿拉伯数字 1 2 3 4 5 6 7 8 9输入字符只包含小写字母,所有字符都可以正好匹配运行时间限制:无限制内存限制:无限制输入:一行字符串,长度小于1000输出:一行字符(数字)串样例输入:yiersansi样例输出:12341.转载请标明出处,原文地址:/hackbuteer1/article/details/392537672.#include<iostream>3.#include<cstdio>ing namespace std;5.6.void solve(char *str , int len)7.{8.int i;9.10. for(i = 0 ; i < len ; )11. {12. switch(str[i])13. {14. case 'y':15. putchar('1');16. i += 2;17. break;18. case 'e':19. putchar('2');20. i += 2;21. break;22. case 's':23. if(str[i + 1] == 'a')24. {25. putchar('3');26. i += 3;27. }28. else29. {30. putchar('4');31. i += 2;32. }33. break;34. case 'w':35. putchar('5');36. i += 2;37. break;38. case 'l':39. putchar('6');40. i += 3;41. break;42. case 'q':43. putchar('7');44. i += 2;45. break;46. case 'b':47. putchar('8');48. i += 2;49. break;50. case 'j':51. putchar('9');52. i += 3;53. break;54. }55. }56. printf("\n");57.}58.59.int main(void)60.{61. int len;62. char str[1000];63.64. while(scanf("%s" , str) != EOF)65. {66. len = strlen(str);67. solve(str , len);68. }69. return 0;70.}第二题:去除重复字符并排序运行时间限制:无限制内容限制:无限制输入:字符串输出:去除重复字符并排序的字符串样例输入:aabcdefff样例输出:abcdef1.转载请标明出处,原文地址:/hackbuteer1/article/details/392537672.#include<iostream>3.#include<cstdio>4.#include<memory>ing namespace std;6.7.void solve(char *str , int len)8.{9.int i , hash[256];10. memset(hash , 0 , sizeof(hash));11.12. for(i = 0 ; i < len ; ++i)13. {14. if(0 == hash[str[i]])15. hash[str[i]] = 1;16. }17. for(i = 0 ; i < 256 ; ++i)18. {19. if(0 != hash[i])20. putchar(i);21. }22. printf("\n");23.}24.25.int main(void)26.{27. int len;28. char str[1000];29.30. while(scanf("%s" , str) != EOF)31. {32. len = strlen(str);33. solve(str , len);34. }35. return 0;36.}第三题:等式变换输入一个正整数X,在下面的等式左边的数字之间添加+号或者-号,使得等式成立。

1 2 3 4 5 6 7 8 9 = X比如:12-34+5-67+89 = 51+23+4-5+6-7-8-9 = 5请编写程序,统计满足输入整数的所有整数个数。

输入:正整数,等式右边的数字输出:使该等式成立的个数样例输入:5样例输出:211.转载请标明出处,原文地址:/hackbuteer1/article/details/392537672.#include<iostream>3.#include<cstdio>ing namespace std;5.6.int ops[21];7.const char sym[3] = {'+' , '-' , ' '};8.int result , num;9.10.void dfs(int layer, int currentResult, int lastOp, intlastSum)11.{12. lastSum *= (layer > 9) ? 100 : 10;13. lastSum += layer;14. if(layer == 9)15. {16. currentResult += (lastOp) ? (-1 * lastSum) : lastSum;17. if(currentResult == result)18. {19. ++num;20. printf("1");21. for(int i = 2 ; i <= 9 ; ++i)22. {23. if(sym[ops[i-1]] != ' ')24. printf(" %c ", sym[ops[i-1]]);25. printf("%d", i);26. }27. printf(" = %d\n" , result);28. }29. return;30. }31. ops[layer] = 2;32. dfs(layer + 1 , currentResult , lastOp , lastSum); //Continue33. currentResult += (lastOp)? (-1 * lastSum) : lastSum;34. ops[layer] = 0;35. dfs(layer + 1 , currentResult , 0 , 0); //Plus36. ops[layer] = 1;37. dfs(layer + 1 , currentResult , 1 , 0); //Minus38.}39.40.int main(void)41.{42. while(scanf("%d", &result) != EOF)43. {44. num = 0;45. dfs(1 , 0 , 0 , 0);46. printf("%d\n" , num);47. }48. return 0;49.}。

相关主题