您的位置:首页 > 其它

2017 校招华为上机题

2017-03-23 21:53 274 查看
1. 给定一个字符串,把字符串内的字母转换成该字母的下一个字母, a 换成b,z 换成a,Z 换成A,
如aBf 转换成bCg,字符串内的其他字符不改变,给定函数,编写函数
void Stringchang(const char*inpu,char*output)
其中input 是输入字符串, output 是输出字符串

1 #include<iostream>
2 #include<cctype>
3 #include<cstring>
4 using namespace std;
5 void stringchang(const char*, char*);
6 int main()
7 {
8     char input[100],output[100];                      //不用再使用for循环来输入字符串数组了
9     getline(cin,input);                          //可以直接使用getline()的方法既可以直接输入字符串数组,也可以直接输入string类对象
10     stringchang(input,output);
11     cout<<output<<endl;
12     return 0;
13 }
14 void stringchang(const char* input, char* output)
15 {
16     int m=strlen(input),n=0;
17     for(int i=0;i<m;i++)
18     {
19         if (isalpha(input[i]))
20         {
21             if (input[i]== 'z')
22             output[n++]= 'a';
23             else if (input[i]== 'Z')
24             output[n++]= 'A';
25             else
26             output[n++]=input[i]+1;
27         }
28     else
29     output[n++]=input[i];
30    }
31   output
= '\0';
32 }


2. 比较一个数组的元素是否为回文数组

1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6   string str;
7   getline(cin,str);                //使用getline的方法直接将string类对象直接输入,不用再考虑1个1个输入了
8   int m=str.size();
9   for(int i=0;i<m/2;i++)
10   {
11     if (str[i]!=str[m-1-i])
12     {
13       cout<<"NO" <<endl;
14       return 0;
15     }
16   }
17   cout<<"YES" <<endl;
18   return 0;
19 }


3.判断回文数,是返回1,不是返回0。

1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6   int num;
7   cin>>num;
8   string str;
9   while (num>0)
10   {
11     str.push_back(num%10+'0');
12     num/=10;
13   }
14   int m=str.size();
15   for(int i=0;i<m/2;i++)
16   {
17     if (str[i]!=str[m-1-i])
18     {
19       cout<< "0" <<endl;
20       return 0;
21     }
22   }
23   cout<<"1" <<endl;
24   return 0;
25 }


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

字符,将非首次出现的字符过滤掉。
比如字符串“ abacacde”过滤结果为“ abcde”。
示例
输入:“ deefd” 输出:“ def ”
输入:“ afafafaf ” 输出:“ af ”
输入:“ pppppppp” 输出:“ p”

1 #include<iostream>
2 #include<string>
3 using namespace std;
4 int main()
5 {
6   string in_str,out_str;
7   cin>>in_str;
8   for(int i=0;i<n;i++)
9    {
10       int count=0;
11       for(int j=0;j<i;j++)
12       {
13         if(in_str[i] ==in_str[j])
14          {
15            count++;
16          }
17
18       }
    }
19        if(count==0)
20        {
21            out_str.push_back(in_str[i]);
22        }
23    cout<<out_str<<endl;
24    return 0;
25 }


5.利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。

比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。

给定一个string iniString为待压缩的串(长度小于等于10000),保证串内字符均由大小写英文字母组成,返回一个string,为所求的压缩后或未变化的串。

测试样例 "aabcccccaaa"
返回:"a2b1c5a3"

   "welcometonowcoderrrrr"

返回:"welcometonowcoderrrrr"


1 class Zipper {
2 public:
3     string zipString(string iniString)
4     {
6       string res;
7       for(int i = 0; i < iniString.length() - 1; i++)
8       {
9          int count = 1;
10          while(iniString[i] == iniString[i+1])                            //和上一题类似,其实也可以用两个for循环,但就需要定义两个i,j变量,
11          {
12              ++count;                               //其实while=for+if,这样就可以只用1个变量。
13              ++i;                                                        //所以典型的结构:for+for+if=for+while
14          }
15           res.push_back(iniString[i]);
16           res+=to_string(count);
17       }
18       return iniString.size() >= res.size() ? res :iniString;
19     }
20 };


6.数组中数字都两两相同,只有一个不同,找出该数字:

1 #include<iostream>
2 using namespace std;
3 int main()           //用异或,相同的话为0,0异或一个数等于他本身
4 {
5   int num[100],n=-1;
6   while (cin>>num[++n]);
{
7     int temp=num[0];
8     for(int i=1;i<n;i++)
9     temp=temp^num[i];    
    }
10   cout<<temp<<endl;
11   return 0;
12 }


7.数组中数字两两相同,有两个不同,找出这两个

1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5   int num[100],n=-1,a[2],flag=0,m=0;
6   while(cin>>num[++n]);
7   for(int i=0;i<n;i++)
8   {
9     flag=0;
10     for(int j=0;j<n;j++)
11     {
12       if (i!=j&&num[i]==num[j])
13       flag=1;
14     }
15   if (flag==0)
16   a[m++]=num[i];
17   if (m==2)
18   break;
19 }
20   cout<<a[0]<< " " <<a[1]<<endl;
21   return 0;
22 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: