题解 | #字符游戏#
字符游戏
https://www.nowcoder.com/practice/381e9c03e6b44c058d81d41f5da8386d
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(0); // 标准输入流! cin.tie(0); cout.tie(0); string a,b; getline(cin,a); getline(cin,b); string c; int la(0),ra(a.size()-1),lb(0),rb(b.size()-1); bool flaga(true),flagb(true); // 正向? char cha,chb; while(la <= ra && lb <= rb){ cha = flaga ? a[la] : a[ra]; chb = flagb ? b[lb] : b[rb]; if(cha < chb){ c.push_back(cha); if(flaga) la++,flaga = false; else ra--,flaga = true; }else { c.push_back(chb); if(flagb) lb++,flagb = false; else rb--,flagb = true; } } while(la <= ra){ c.push_back(flaga ? a[la] : a[ra]); if(flaga) la++,flaga = false; else ra--,flaga = true; } while(lb <= rb){ c.push_back(flagb ? b[lb] : b[rb]); if(flagb) lb++,flagb = false; else rb--,flagb = true; } cout << c << endl; return 0; }