题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
bool alpha(char c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
}
int main() {
string str, str_n, str2;
//cin >> str;
getline(cin, str);
str2 = str;
vector<char> v1, v2;
//建一个按照字典排序的字母容器
for (int i = 0; i < 26; ++i) {
v1.push_back(char('a' + i));
}
//得到一个相同字母,大小写按照原来输入顺序排列的容器
for (auto v : v1) {
for (int i = 0; i < str.length(); ++i) {
if (tolower(str[i]) == v) {
v2.push_back(str[i]);
}
}
}
int count = 0;
//非字母保持原样,其他的从上面得到的容器中取用
for (int i = 0; i < str2.length(); ++i) {
if (alpha(str2[i])) {
str_n += v2[count];
++count;
} else {
str_n += str2[i];
}
}
//得到最终结果
cout << str_n;
return 0;
}
// 64 位输出请用 printf("%lld")