华为机试在线训练C++版(21~30)

21.简单密码

在这里插入图片描述
贴一个很6的解法,因为密码的组成就那么多,所以直接“查找”即可:

#include<iostream>
#include<string>
using namespace std;
const string dict1="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string dict2="bcdefghijklmnopqrstuvwxyza22233344455566677778889999";
 
char Char_Change(char a){
    for(int i=0;i<dict1.size();i++)
        if(dict1[i]==a) return dict2[i];
    return a;
}
 
int main(){
    //string data="YUANzhi1987";
    string data;
    while(getline(cin,data)){
        for(int i=0;i<data.size();i++)
            data[i] = Char_Change(data[i]);
        cout<<data<<endl;
    }
    return 0;
}

正常的写法:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str;
    while (cin >> str){
        int length = str.size();
        string output(str);
        for (int i = 0; i<length; i++){
            if (str[i] >= '0' && str[i] <= '9')
                output[i] = str[i];
            else if (str[i] >= 'A' && str[i] <= 'Z'){
                if (str[i] == 'Z')
                    output[i] = 'a';
                else
                    output[i] = 'a' + str[i] - 'A' + 1;
            }
            else if (str[i] >= 'a' && str[i] <= 'z'){
                if (str[i] == 'z')
                    output[i] = '0' + 9;
                else if (str[i] >= 'p' && str[i] <= 's')
                    output[i] = '0' + 7;
                else if (str[i] >= 't' && str[i] <= 'v')
                    output[i] = '0' + 8;
                else if (str[i] >= 'w' && str[i] <= 'z')
                    output[i] = '0' + 9;
                else {
                    int num = (str[i] - 'a') / 3 + 2;
                    output[i] = '0' + num;
                }
            }
        }
        cout << output << endl;
    } 
    return 0;
}

22.汽水瓶

在这里插入图片描述
小学的数奥题2333,直接就是可以换到一半的瓶子:

#include <iostream>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        cout<<n/2<<endl;
    }
    return 0;
}

23.删除字符串中出现次数最少的字符

在这里插入图片描述
和之前的几个题目同样的思路,利用数组来统计个数:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str;
    while(cin>>str){
        int a[26]={0}; //次数全部为0
        int min,m=str.size();
        for(int i=0;i<m;++i){
            a[str[i]-'a']++;  //次数统计
        }           
        min=a[str[0]-'a'];    //暂且令第一个字符出现次数为最小
        for(int i=1;i<m;++i){
            if(a[str[i]-'a']<min){
                min=a[str[i]-'a'];//得到整个字符串的出现的最小次数
            }                
        }               
        for(int i=0;i<m;++i){
             if(a[str[i]-'a']>min){
                  cout<<str[i];//输出超过最小次数的所有字符
             }               
        }           
        cout<<endl;
    }
    return 0;
}

24.合唱队

在这里插入图片描述
动态规划问题,主要是用到递增子序列
思想:所有比m[i]小的数都可以作为倒数第二个数,在这些第二个数中,以哪个数结尾的递增子序列最大,就以那个数作为倒数第二个数 。以本身作为最后一个数,前面没有比它小的,则子序列为1.
在这里插入图片描述
首先计算每个数在最大递增子串中的位置
186 186 150 200 160 130 197 200 quene
1 1 1 2 2 1 3 4 递增计数

然后计算每个数在反向最大递减子串中的位置—>计算反向后每个数在最大递增子串中的位置
200 197 130 160 200 150 186 186 反向quene
1 1 1 2 3 2 3 3 递减计数

然后将每个数的递增计数和递减计数相加
186 186 150 200 160 130 197 200 quene
1 1 1 2 2 1 3 4 递增计数
3 3 2 3 2 1 1 1 递减计数
4 4 3 5 4 2 4 5 每个数在所在队列的人数+1(自己在递增和递减中被重复计算)

如160这个数
在递增队列中有2个人数
150 160
在递减队列中有2个人数
160 130
那么160所在队列中就有3个人
150 160 130

每个数的所在队列人数表达就是这个意思
总人数 - 该数所在队列人数 = 需要出队的人数

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
void calIncSub(vector<int> quene, vector<int> &Num){
    for(int i=1;i<quene.size();i++)
        for(int j=i-1;j>=0;j--)
            if(quene[j]<quene[i] && Num[i]<Num[j]+1)  //找到前面比当前小的,且【能获得的最大子串计数】
                Num[i]=Num[j]+1;
}
 
int main(){
    int n;
    int h;
     
    while(cin>>n){
        vector<int> quene;
        vector<int> incNum(n,1);  //初始化为n个1
        vector<int> decNum(n,1); 
        vector<int> totalNum;
        for(int i=0;i<n;i++){
            cin >> h;
            quene.push_back(h);   
        }
        calIncSub(quene,incNum);    //找递增子串计数
        reverse(quene.begin(),quene.end()); //翻转,即找反向的子串计数
        calIncSub(quene,decNum);
        reverse(decNum.begin(),decNum.end());   //反向递增即正向递减
        int max=0;
        for(int i=0;i<n;i++){
            totalNum.push_back(incNum[i]+decNum[i]);
            if(totalNum[i]>max)
                max=totalNum[i];
        }
        cout << n-max+1 <<endl;
    }  
    return 0;
}

25.数据分类处理

在这里插入图片描述

题目描述这么多,需求就是:将规则数组排序(从小到大),并去重。
遍历输入数组,检查输入数组的每个元素是是否包含规则数组中的数字i,如果包含则将输入数组元素位置和元素输出到最终结果中。
也是纯粹考察代码实现的一道题:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool match(int m, int n) {
    string str1 = to_string(m);
    string str2 = to_string(n);
    int pos = str2.find(str1);
    if (pos != -1)
        return true;
    else
        return false;
}
int main() {
    int m, n;
    while (cin >> m) {
        vector<int> I;
        vector<int> R;
        for (int i = 0; i < m; i++) {
            int  temp;
            cin >> temp;
            I.push_back(temp);
        }
        cin >> n;
        for (int i = 0; i < n; i++) {
            int  temp;
            cin >> temp;
            R.push_back(temp);
        }
        sort(R.begin(), R.end());//排序
        R.erase(unique(R.begin(), R.end()), R.end());//去重

        vector<int> index;
        vector<int> value;
        vector<int> cnt;
        vector<int> index1;
        for (int i = 0; i < R.size(); i++) {
            int cnt1 = 0;//每个R[i]在I中存在的个数
            for (int pos = 0; pos < I.size(); pos++) {//遍历I        
                if (match(R[i], I[pos])) {//如果R[i]在I中
                    cnt1++;//计数++
                    index.push_back(pos);//把相应位置压入index
                    value.push_back(I[pos]);//把在I中相应位置的值压入value
                }
            }
            if (cnt1 != 0) {//判断每个R[i]在I中的个数是否为0
                cnt.push_back(cnt1);//把R[i]在I中对应找到的个数压入cnt
                index1.push_back(i);//把R中每个位置压入index1
            }
        }
        int j = 0;
        cout << 2 * index.size() + index1.size() + cnt.size() << ' ';//2*index.size()是一个位置+位置的值
        for (int i = 0; i < cnt.size(); i++) {
            cout << R[index1[i]] << ' ' << cnt[i] << ' '; //把R中相应的值和在I中找到的个数输出    
            while (cnt[i]-- > 0) {//循环输出R中R[i]在I中的位置和值
                cout << index[j] << ' ' << value[j];
                if (i == cnt.size() - 1 && cnt[i] == 0) {
                    cout << endl;
                }
                else {
                    cout << ' ';
                }
                j++;
            }
        }
    }
    return 0;
}

26.字符串排序

在这里插入图片描述
一般的思路就是冒泡排序思想:

#include<iostream>
#include<string>
using namespace std;
int main() {
    string str;
    while (cin>>str) {
        for (int i = 0; i < str.size() - 1; i++) {
            int temp = 0;
            for (int j = 0; j < str.size() - i; j++) {
                if (!isalpha(str[temp])) {
                    temp = j;
                    continue;
                }
                if (isalpha(str[j])) {
                    if (tolower(str[temp]) > tolower(str[j]))
                        swap(str[temp], str[j]);
                    temp = j;
                }
            }
        }
        cout << str << endl;
    }
    return 0;
}

另一种思路:共有26个字母所以最外层的循环是26,每次循环从字符串中只选择一个字母并如栈,这样经过26次循环字符串中a-z就按顺序入栈了,再从头到尾输出即可了。

#include<vector>
#include<iostream>
#include<string>
using namespace std;
int main() {
    string str;
    vector<char> tempChar;
    while (getline(cin, str)) {
        tempChar.clear();
        int len = str.size();
        for (int j = 0; j < 26; j++) {
            for (int i = 0; i < len; i++) {
                if (str[i] - 'a' == j || str[i] - 'A' == j) {
                    tempChar.push_back(str[i]);
                }
            }
        }
        for (int i = 0, k = 0; (i < len) && k < tempChar.size(); i++) {
            if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
                str[i] = tempChar[k++];
        }
        cout << str << endl;
    }
    return 0;
}

27.查找兄弟单词

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这个题目不难。是问题的描述让人很是糊涂。
正确的描述应该是这样的:
输入描述:
先输入字典中单词的个数n,再输入n个单词作为字典单词。
再输入一个单词,查找其在字典中兄弟单词的个数m
再输入数字k

输出描述:
根据输入,输出查找到的兄弟单词的个数m
然后输出查找到的兄弟单词的第k个单词。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//查找兄弟单词的函数接口,兄弟单词:单词的字母及其个数相同,但字母的排列顺序不全相同
int Search_Brother_Word(int num) {
    string str; //字典里面的单词(小写英文字符)
    string word; //指定的单词
    vector <string> vec; //用一个向量来存储单词(形成单词字典)
    vector <string> vec_BW; //用一个向量来存储兄弟单词(形成兄弟单词字典),BW 代表 Brother Word
    int BW_index; //序号,用于查找指定单词的所有兄弟单词中序号所对应的兄弟单词(序号减一,因为从零开始)
    //输入单词,形成单词字典
    for (int i = 0; i < num; i++) {
        cin >> str;
        vec.push_back(str);
    }
    sort(vec.begin(), vec.end()); //输入的多个单词按字典序排列
    cin >> word; //指定单词,当然这个单词可能在、也可能不在原来的字典里面
    cin >> BW_index; //指定单词 word 的所有兄弟单词中排在第 BW_index 的单词对应序号 BW_index - 1
    //判断字典中的单词是否为指定单词的兄弟单词,若是,则将其写入兄弟单词向量 vec_BW
    string word_copy = word; //拷贝一份指定的单词,作为基准比较(字母顺序未变动)
    sort(word.begin(), word.end()); //指定的单词的字母按字典序排列
    //遍历单词字典,寻找指定单词的兄弟单词并记录在兄弟单词字典中
    for (int i = 0; i < vec.size(); i++) {
        //先判断字典中的单词的长度和指定单词的长度是否一致
        if (vec[i].size() == word.size()) {
            //长度相同且字母顺序不同的单词,才可能是兄弟单词,这里反复用单词的拷贝来作为基准
            if (vec[i] != word_copy) {
                string copy = vec[i]; //提前拷贝一份字典中的单词,作为后续写入兄弟单词字典的样本(需要的话)
                sort(vec[i].begin(), vec[i].end()); //字典中的单词的字母按字典序排列
                //原本不相同的单词,经过字母排序后相同,才是互为兄弟单词
                if (vec[i] == word) {
                    vec_BW.push_back(copy);
                }
            }
        }
    }
    //分类输出结果
    //兄弟单词字典为空
    if (vec_BW.size() == 0) {
        cout << 0 << endl;
    }
    //兄弟单词字典不为空,但待查找的兄弟单词不存在
    else if (((vec_BW.size() > 0) && (vec_BW.size() < BW_index)) || ((vec_BW.size() > 0) && (BW_index < 1))) {
        cout << vec_BW.size() << endl;
    }
    //兄弟单词字典不为空,且待查找的兄弟单词存在
    else if ((vec_BW.size() > 0) && (vec_BW.size() >= BW_index) && (BW_index >= 1)) {
        cout << vec_BW.size() << '\n' << vec_BW[BW_index - 1] << endl;
    }
    return 0;
}
//主函数
int main() {
    int num;
    while (cin >> num) {
        Search_Brother_Word(num);
    }
    return 0;
}

28.素数伴侣

在这里插入图片描述
参考代码

29.字符串加解密

在这里插入图片描述
跟之前的简单密码那一题有异曲同工之妙:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const string helper1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const string helper2 = "BCDEFGHIJKLMNOPQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza1234567890";
void Encrypt(string str) {
    string res;
    for (int i = 0; i < str.size(); i++) {
        res += helper2[helper1.find(str[i])];
    }
    cout << res << endl;
}
void unEncrypt(string str) {
    string res;
    for (int i = 0; i < str.size(); i++) {
        res += helper1[helper2.find(str[i])];
    }
    cout << res << endl;
}
int main() {
    string str1, str2;
    while (getline(cin, str1)) {
        getline(cin, str2);
        Encrypt(str1);
        unEncrypt(str2);
    }
    return 0;
}

30.字符串合并处理

在这里插入图片描述

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const string helper1 = "0123456789abcdefABCDEF";
const string helper2 = "084C2A6E195D3B7F5D3B7F";
int main() {
    string str1, str2;
    while (cin >> str1 >> str2) {
        string s, s1, s2;
        s = str1 + str2;
        int len = s.size();
        for (int i = 0; i < len; ++i) {
            if (i % 2 == 0)
                s1 += s[i];
            else
                s2 += s[i];
        }
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        s.clear();
        for (int i = 0, j = 0, k = 0; i < len; ++i) {
            if (i % 2 == 0)
                s += s1[j++];
            else
                s += s2[k++];
        }
        for (int i = 0; i < len; ++i) {
            int n = helper1.find(s[i]);
            if (n != -1)
                s[i] = helper2[n];
        }
        cout << s << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值