华为机试:字符串排序
1.英文字母不分大小写升序排序
2.其他字符保持原位
例:BcdAaAtTypE------>AaABcdEptTy
#include <iostream> #include <string> #include <cctype> #include <algorithm> using namespace std; bool cmp(const char x, const char y) { return tolower(x) < tolower(y); } int main() { string input; getline(cin, input); string Sort; for (char ch : input) { if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { Sort.push_back(ch); } else { continue; } } stable_sort(Sort.begin(), Sort.end(), cmp); //要稳定的排序相对顺序才不会变 string ans; auto it = Sort.begin(); for (char ch : input) { if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { ans.push_back(*it); it++; } else { ans.push_back(ch); } } cout << ans << endl; return 0; }