题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include<bits/stdc++.h>
using namespace std;
int sort_A(vector<char>& vec);
bool comp(char a,char b){
if(a-b=='A'-'a'||b-a=='A'-'a')
return true;
a = toupper(a);b=toupper(b);
return a<b;
}
int main(){
string str;
while(getline(cin, str)){
vector<char> vec;
vector<int> indexs;
for(int i=0;i<str.size();++i){
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
vec.push_back(str[i]);
indexs.push_back(i);
}
}
//stable_sort(vec.begin(), vec.end(),comp);
sort_A(vec);
for(int i =0;i<indexs.size();++i){
str[indexs[i]] = vec[i];
}
cout<<str<<endl;
}
return 0;
}
//冒泡排序
int sort_A(vector<char>& vec){
int n=vec.size();
for(int i=1;i<n;++i){
for(int j=0;j<n-i;++j){
int c1=toupper(vec[j]);
int c2=toupper(vec[j+1]);
if(c1>c2){
swap(vec[j], vec[j+1]);
}
}
}
return 1;
}