2015届华为校园招聘机试题

第一题(60分):
       按要求分解字符串,输入两个数M,N;M代表输入的M串字符串,N代表输出的每串字符串的位数,不够补0。例如:输入2,8, “abc” ,“123456789”,则输出为“abc00000”,“12345678“,”90000000”
[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. using namespace std;  
  4.   
  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.   
  11.     for(i = 0 ; i < len ; i += n)  
  12.     {  
  13.         if(len - i < n)  
  14.         {  
  15.              k = n - len + i;  
  16.              for(j = i ; j < len ; ++j)  
  17.                  printf("%c" , str[j]);  
  18.              for(j = 0 ; j < k ; ++j)  
  19.                  putchar('0');  
  20.         }  
  21.         else  
  22.         {  
  23.             for(j = i ; j < i + n ; ++j)  
  24.                 printf("%c" , str[j]);  
  25.         }  
  26.         putchar(' ');  
  27.     }  
  28.     printf("\n");  
  29. }  
  30.   
  31. int main(void)  
  32. {  
  33.     int i , m , n , len;  
  34.     char str[1000];  
  35.   
  36.     while(scanf("%d %d", &m , &n) != EOF)  
  37.     {  
  38.         for(i = 0 ; i < m ; ++i)  
  39.         {  
  40.             scanf("%s" , str);  
  41.             len = strlen(str);  
  42.             solve(str , n , len);  
  43.         }  
  44.     }  
  45.     return 0;  
  46. }  
第一题:拼音转数字
输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:
描述:      拼音        yi  er  san  si  wu  liu  qi  ba  jiu
      阿拉伯数字        1   2   3      4   5    6    7   8   9
输入字符只包含小写字母,所有字符都可以正好匹配

运行时间限制:无限制
内存限制:       无限制
输入:              一行字符串,长度小于1000
输出:              一行字符(数字)串
样例输入:       yiersansi
样例输出:       1234

[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. using namespace std;  
  4.   
  5. void solve(char *str , int len)  
  6. {  
  7.     int i;  
  8.   
  9.     for(i = 0 ; i < len ; )  
  10.     {  
  11.         switch(str[i])  
  12.         {  
  13.         case 'y':  
  14.             putchar('1');  
  15.             i += 2;  
  16.             break;  
  17.         case 'e':  
  18.             putchar('2');  
  19.             i += 2;  
  20.             break;  
  21.         case 's':  
  22.             if(str[i + 1] == 'a')  
  23.             {  
  24.                 putchar('3');  
  25.                 i += 3;  
  26.             }  
  27.             else  
  28.             {  
  29.                 putchar('4');  
  30.                 i += 2;  
  31.             }  
  32.             break;  
  33.         case 'w':  
  34.             putchar('5');  
  35.             i += 2;  
  36.             break;  
  37.         case 'l':  
  38.             putchar('6');  
  39.             i += 3;  
  40.             break;  
  41.         case 'q':  
  42.             putchar('7');  
  43.             i += 2;  
  44.             break;  
  45.         case 'b':  
  46.             putchar('8');  
  47.             i += 2;  
  48.             break;  
  49.         case 'j':  
  50.             putchar('9');  
  51.             i += 3;  
  52.             break;  
  53.         }  
  54.     }  
  55.     printf("\n");  
  56. }  
  57.   
  58. int main(void)  
  59. {  
  60.     int len;  
  61.     char str[1000];  
  62.   
  63.     while(scanf("%s" , str) != EOF)  
  64.     {  
  65.         len = strlen(str);  
  66.         solve(str , len);  
  67.     }  
  68.     return 0;  
  69. }  
第二题:去除重复字符并排序
运行时间限制:无限制
内容限制:       无限制
输入:              字符串
输出:              去除重复字符并排序的字符串
样例输入:       aabcdefff
样例输出:       abcdef
[cpp]  view plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory>  
  4. using namespace std;  
  5.   
  6. void solve(char *str , int len)  
  7. {  
  8.     int i , hash[256];  
  9.     memset(hash , 0 , sizeof(hash));  
  10.   
  11.     for(i = 0 ; i < len ; ++i)  
  12.     {  
  13.         if(0 == hash[str[i]])  
  14.             hash[str[i]] = 1;  
  15.     }  
  16.     for(i = 0 ; i < 256 ; ++i)  
  17.     {  
  18.         if(0 != hash[i])  
  19.             putchar(i);  
  20.     }  
  21.     printf("\n");  
  22. }  
  23.   
  24. int main(void)  
  25. {  
  26.     int len;  
  27.     char str[1000];  
  28.   
  29.     while(scanf("%s" , str) != EOF)  
  30.     {  
  31.         len = strlen(str);  
  32.         solve(str , len);  
  33.     }  
  34.     return 0;  
  35. }  
第三题:等式变换
输入一个正整数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 plain copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. using namespace std;  
  4.   
  5. int ops[21];  
  6. const char sym[3] = {'+' , '-' , ' '};  
  7. int result , num;  
  8.   
  9. void dfs(int layer, int currentResult, int lastOp, int lastSum)  
  10. {  
  11.     lastSum *= (layer > 9) ? 100 : 10;  
  12.     lastSum += layer;  
  13.     if(layer == 9)  
  14.     {  
  15.         currentResult += (lastOp) ? (-1 * lastSum) : lastSum;  
  16.         if(currentResult == result)  
  17.         {  
  18.             ++num;  
  19.             printf("1");  
  20.             for(int i = 2 ; i <= 9 ; ++i)  
  21.             {  
  22.                 if(sym[ops[i-1]] != ' ')  
  23.                     printf(" %c ", sym[ops[i-1]]);  
  24.                 printf("%d", i);  
  25.             }  
  26.             printf(" = %d\n" , result);  
  27.         }  
  28.         return;  
  29.     }  
  30.     ops[layer] = 2;  
  31.     dfs(layer + 1 , currentResult , lastOp , lastSum);   //Continue  
  32.     currentResult += (lastOp)? (-1 * lastSum) : lastSum;  
  33.     ops[layer] = 0;  
  34.     dfs(layer + 1 , currentResult , 0 , 0);  //Plus  
  35.     ops[layer] = 1;  
  36.     dfs(layer + 1 , currentResult , 1 , 0);  //Minus  
  37. }  
  38.   
  39. int main(void)  
  40. {  
  41.     while(scanf("%d", &result) != EOF)  
  42.     {  
  43.         num = 0;  
  44.         dfs(1 , 0 , 0 , 0);  
  45.         printf("%d\n" , num);  
  46.     }  
  47.     return 0;  
  48. }  
  1. 原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值