1、HJ14 字符串排序(简单)


#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string str;
vector<string> vec;
int num;
cin >> num;
while (cin >> str) {
vec.push_back(str);
}
sort(vec.begin(), vec.end(), [](string s1, string s2){return s1.compare(s2) <= 0;});
for (int i = 0; i < vec.size(); ++i) {
cout << vec[i] << endl;
}
}
// 64 位输出请用 printf("%lld")
2、HJ26 字符串排序(中等)


#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
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;
}
本文提供了两个不同难度级别的字符串排序算法示例,包括简单的字符串数组排序和中等难度的字符串内部字符排序,通过C++实现并详细展示了代码逻辑。
1052

被折叠的 条评论
为什么被折叠?



