题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
int find(string dictory,char ch)
{
    for(int i = 0; i < dictory.size();i++)
    {
        if(ch>='a'&&ch<='z')
        {
            ch = char(ch-32);
        }
        if(ch == dictory[i])
            return i;
    }
    return -1;
}

int main() {
    string str1,str2;
    getline(cin,str1,' ');
    getline(cin,str2, ' ');
    string str = str1+str2;
    stringstream oddss;
    stringstream evenss;
    for(int i = 0; i < str.size();i++)
    {
        if(i%2==0)
            evenss<<str[i];
        else
            oddss<<str[i];
    }
    string even,odd;
    getline(evenss,even);
    getline(oddss, odd);
    sort(even.begin(),even.end());
    sort(odd.begin(),odd.end());
    string strAfterSort = str;
    for(int i = 0,t = 0,j = 0; i < str.size();i++)
    {
        if(i%2==0)
        {
            strAfterSort[i] = even[t];
            t++;
        }
        else 
        {
            strAfterSort[i] = odd[j];
            j++;
        }  
    }

    string dictory = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(int i = 0; i<strAfterSort.size();i++)
    {
        int value = find(dictory,strAfterSort[i]);
        if(value == -1)
            continue;
        if(value > 15)
        {
            cout<<strAfterSort[i];
            continue;
        }
        string binary(4,'0');
        if(value == 0)
            binary = "0000";
        else
        {
            
            if(value == 1)
                binary = "1000";
                else
                 {
                    int count = 0;
                    while(value!=0)
                    {
                        binary[count++] = char(value%2+48);
                        value/=2;
                    }
                 }
            
        }
        int valueTurn = (binary[0]-48)*8+(binary[1]-48)*4+(binary[2]-48)*2+binary[3]-48;
        cout<<dictory[valueTurn];
        

    }
    

    

    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务