华为2014--算法汇总

1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。

比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
            lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
 

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”

main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出
当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。

一定要保证编译运行不受影响


  1. //华为第一题 19:19-19:36  17分钟     
  2. #include <iostream>     
  3. #include <cassert>     
  4.     
  5. using namespace std;    
  6.     
  7. bool g_flag[26];    
  8. void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)    
  9. {    
  10.   assert(pInputStr != NULL);    
  11.   int i = 0;    
  12.   if (pInputStr == NULL || lInputLen <= 1)    
  13.   {    
  14.       return;    
  15.   }    
  16.   const char *p = pInputStr;    
  17.   while(*p != '\0')    
  18.   {    
  19.      if (g_flag[(*p - 'a')])    
  20.      {    
  21.          p++;    
  22.      }else{    
  23.          pOutputStr[i++] = *p;    
  24.          g_flag[*p - 'a'] = 1;    
  25.          p++;    
  26.      }    
  27.   }    
  28.   pOutputStr[i] = '\0';    
  29. }    
  30. int main()    
  31. {    
  32.     memset(g_flag,0,sizeof(g_flag));    
  33.     char input[] = "abacacde";    
  34.     char *output = new char[strlen(input) + 1];    
  35.     stringFilter(input,strlen(input),output);    
  36.     cout<<output<<endl;    
  37.     delete output;    
  38.     return 0;    
  39. }    
//华为第一题 19:19-19:36  17分钟  
#include <iostream>  
#include <cassert>  
  
using namespace std;  
  
bool g_flag[26];  
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)  
{  
  assert(pInputStr != NULL);  
  int i = 0;  
  if (pInputStr == NULL || lInputLen <= 1)  
  {  
      return;  
  }  
  const char *p = pInputStr;  
  while(*p != '\0')  
  {  
     if (g_flag[(*p - 'a')])  
     {  
         p++;  
     }else{  
         pOutputStr[i++] = *p;  
         g_flag[*p - 'a'] = 1;  
         p++;  
     }  
  }  
  pOutputStr[i] = '\0';  
}  
int main()  
{  
    memset(g_flag,0,sizeof(g_flag));  
    char input[] = "abacacde";  
    char *output = new char[strlen(input) + 1];  
    stringFilter(input,strlen(input),output);  
    cout<<output<<endl;  
    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”

  1. //华为第二题 19:40 - 20:10 中间耽误3分钟     
  2. #include <iostream>     
  3. #include <cassert>     
  4.     
  5. using namespace std;    
  6.     
  7. void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)    
  8. {    
  9.   const char *p = pInputStr;    
  10.   int num = 1;    
  11.   int i = 0;    
  12.   p++;    
  13.   while(*p != NULL)    
  14.   {    
  15.       while(*p == *(p-1)&& *p != NULL)    
  16.       {    
  17.        num++;    
  18.        p++;    
  19.       }    
  20.       if (num > 1)    
  21.       {    
  22.            int size = 0;    
  23.            int temp = num;    
  24.            while(num)             //计算位数     
  25.            {    
  26.              size++;    
  27.              num /= 10;    
  28.            }    
  29.            num = 1;    
  30.     
  31.            for (int j = size; j > 0; j--)    
  32.            {    
  33.                pOutputStr[i+j-1] = '0'+ temp%10;    
  34.                temp /= 10;    
  35.            }    
  36.            i +=size;    
  37.            pOutputStr[i++] = *(p-1);    
  38.            p++;    
  39.       }else{    
  40.           pOutputStr[i++] = *(p-1);    
  41.           p++;    
  42.       }    
  43.   }    
  44.   pOutputStr[i] = '\0';    
  45. }    
  46.     
  47. int main()    
  48. {    
  49.     char input[] = "cccddecc";    
  50.     char *output = new char[strlen(input) + 1];    
  51.     stringZip(input,strlen(input),output);    
  52.     cout<<output<<endl;    
  53.     return 0;    
  54. }    
//华为第二题 19:40 - 20:10 中间耽误3分钟  
#include <iostream>  
#include <cassert>  
  
using namespace std;  
  
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)  
{  
  const char *p = pInputStr;  
  int num = 1;  
  int i = 0;  
  p++;  
  while(*p != NULL)  
  {  
      while(*p == *(p-1)&& *p != NULL)  
      {  
       num++;  
       p++;  
      }  
      if (num > 1)  
      {  
           int size = 0;  
           int temp = num;  
           while(num)             //计算位数  
           {  
             size++;  
             num /= 10;  
           }  
           num = 1;  
  
           for (int j = size; j > 0; j--)  
           {  
               pOutputStr[i+j-1] = '0'+ temp%10;  
               temp /= 10;  
           }  
           i +=size;  
           pOutputStr[i++] = *(p-1);  
           p++;  
      }else{  
          pOutputStr[i++] = *(p-1);  
          p++;  
      }  
  }  
  pOutputStr[i] = '\0';  
}  
  
int main()  
{  
    char input[] = "cccddecc";  
    char *output = new char[strlen(input) + 1];  
    stringZip(input,strlen(input),output);  
    cout<<output<<endl;  
    return 0;  
}  

3. 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。

输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1、操作数为正整数,不需要考虑计算结果溢出的情况。
2、若输入算式格式错误,输出结果为“0”。

要求实现函数: 
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
            lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
 

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“4 + 7”  输出:“11”
输入:“4 - 7”  输出:“-3”
输入:“9 ++ 7”  输出:“0” 注:格式错误

  1. //华为第三题 20:29 - 20:40     
  2. #include <iostream>     
  3.     
  4. using namespace std;    
  5.     
  6. void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)    
  7. {    
  8.  const char *input = pInputStr;    
  9.        char *output = pOutputStr;    
  10.  int sum = 0;    
  11.  int operator1 = 0;    
  12.  int operator2 = 0;    
  13.  char *temp = new char[5];    
  14.  char *ope = temp;    
  15.  while(*input != ' '//获得操作数1     
  16.  {    
  17.      sum = sum*10 + (*input++ - '0');    
  18.  }    
  19.  input++;    
  20.  operator1 = sum;    
  21.  sum = 0;    
  22.     
  23.  while(*input != ' ')    
  24.  {    
  25.      *temp++ = *input++;    
  26.  }    
  27.     
  28.  input++;    
  29.  *temp = '\0';    
  30.     
  31.  if (strlen(ope) > 1 )    
  32.  {    
  33.      *output++ = '0';    
  34.      *output = '\0';    
  35.      return;    
  36.  }    
  37.     
  38.  while(*input != '\0'//获得操作数2     
  39.  {    
  40.      sum = sum*10 + (*input++ - '0');    
  41.  }    
  42.  operator2 = sum;    
  43.  sum = 0;    
  44.     
  45.  switch (*ope)    
  46.  {    
  47.  case '+':itoa(operator1+operator2,pOutputStr,10);    
  48.      break;    
  49.  case '-':itoa(operator1-operator2,pOutputStr,10);    
  50.     break;    
  51.  default:    
  52.      *output++ = '0';    
  53.      *output = '\0';    
  54.      return;    
  55.  }    
  56. }    
  57.     
  58. int main()    
  59. {    
  60.     char input[] = "4 - 7";    
  61.     char output[] = "    ";    
  62.     arithmetic(input,strlen(input),output);    
  63.     cout<<output<<endl;    
  64.     return 0;    
  65. }    
//华为第三题 20:29 - 20:40  
#include <iostream>  
  
using namespace std;  
  
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)  
{  
 const char *input = pInputStr;  
       char *output = pOutputStr;  
 int sum = 0;  
 int operator1 = 0;  
 int operator2 = 0;  
 char *temp = new char[5];  
 char *ope = temp;  
 while(*input != ' ') //获得操作数1  
 {  
     sum = sum*10 + (*input++ - '0');  
 }  
 input++;  
 operator1 = sum;  
 sum = 0;  
  
 while(*input != ' ')  
 {  
     *temp++ = *input++;  
 }  
  
 input++;  
 *temp = '\0';  
  
 if (strlen(ope) > 1 )  
 {  
     *output++ = '0';  
     *output = '\0';  
     return;  
 }  
  
 while(*input != '\0') //获得操作数2  
 {  
     sum = sum*10 + (*input++ - '0');  
 }  
 operator2 = sum;  
 sum = 0;  
  
 switch (*ope)  
 {  
 case '+':itoa(operator1+operator2,pOutputStr,10);  
     break;  
 case '-':itoa(operator1-operator2,pOutputStr,10);  
    break;  
 default:  
     *output++ = '0';  
     *output = '\0';  
     return;  
 }  
}  
  
int main()  
{  
    char input[] = "4 - 7";  
    char output[] = "    ";  
    arithmetic(input,strlen(input),output);  
    cout<<output<<endl;  
    return 0;  
}  


4.输入1--50个数字,求出最小数和最大数的和

  1. //华为2014年机试题1:输入1--50个数字,求出最小数和最大数的和     
  2. //输入以逗号隔开     
  3. #include<stdio.h>     
  4. #define N 50     
  5. void sort(int a[],int n);    
  6. int main(void)    
  7. {    
  8.     
  9.     
  10.     char str[100];    
  11.     int a[N]={0};    
  12.     gets(str);      //要点1:动态的输入1--50个整数,不能确定个数,只能用字符串输入,然后分离出来     
  13.     int i=0;    
  14.     int j=0;    
  15.     int sign=1;    
  16.     while(str[i]!='\0')    
  17.     {    
  18.         if(str[i]!=',')  //输入时要在半角输入     
  19.         {    
  20.     
  21.             if(str[i] == '-')    //要点:2:有负整数的输入     
  22.             {    
  23.                // i++;   //易错点1     
  24.                 sign=-1;    
  25.             }    
  26.             else if(str[i]!='\0'//不用else的话,负号也会减去‘0’     
  27.            {    
  28.                a[j]=a[j]*10 + str[i]-'0'//要点3:输入的可以是多位数     
  29.     
  30.            }    
  31.         }    
  32.         i++;    
  33.         if(str[i]==',' || str[i]=='\0')  //这个判断是在i自加以后     
  34.         {    
  35.              a[j]=a[j]*sign;  //易错点2     
  36.              sign=1;   易错点3     
  37.              j++;    //j就是a数组的个数 范围0到j-1     
  38.         }    
  39.     
  40.     
  41.     }    
  42.     
  43.     sort(a,j);    
  44.     printf("Max number + Min number = %d",a[0]+a[j-1]);    
  45.     
  46.     return 0;    
  47. }    
  48. void sort(int a[],int n)  //选择排序     
  49. {    
  50.     int i,j;    
  51.     int k;    
  52.     int temp;    
  53.     for(i=0;i<n-1;i++)    
  54.     {    
  55.         k=i;    
  56.         for(j=i+1;j<n;j++)    
  57.         {    
  58.             if(a[k]>a[j])    
  59.                 k=j;    
  60.         }    
  61.         if(i!=k)    
  62.         {    
  63.             temp = a[k];    
  64.             a[k] = a[i];    
  65.             a[i] = temp;    
  66.         }    
  67.     }    
  68.     for(i=0;i<n;i++)    
  69.         printf("%-5d",a[i]);    
  70.     puts("");   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值