2015届华为校园招聘机试题 C语言实现

    转载自:原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767

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

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767  
  2. #include<iostream>  
  3. #include<cstdio>  
  4. using 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.         else  
  23.         {  
  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. }  
第一题:拼音转数字
输入是一个只包含拼音的字符串,请输出对应的数字序列。转换关系如下:
描述:      拼音        yi  er  san  si  wu  liu  qi  ba  jiu
      阿拉伯数字        1   2   3      4   5    6    7   8   9
输入字符只包含小写字母,所有字符都可以正好匹配

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

  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767  
  2. #include<iostream>  
  3. #include<cstdio>  
  4. using 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.             else  
  29.             {  
  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
样例输出:       abcdef
  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767  
  2. #include<iostream>  
  3. #include<cstdio>  
  4. #include<memory>  
  5. using 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 = 5
1+23+4-5+6-7-8-9 = 5
请编写程序,统计满足输入整数的所有整数个数。
输入:       正整数,等式右边的数字
输出:       使该等式成立的个数
样例输入:5
样例输出:21
  1. 转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/39253767  
  2. #include<iostream>  
  3. #include<cstdio>  
  4. using 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, int lastSum)  
  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);   //Continue  
  33.     currentResult += (lastOp)? (-1 * lastSum) : lastSum;  
  34.     ops[layer] = 0;  
  35.     dfs(layer + 1 , currentResult , 0 , 0);  //Plus  
  36.     ops[layer] = 1;  
  37.     dfs(layer + 1 , currentResult , 1 , 0);  //Minus  
  38. }  
  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;  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值