题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <iostream> #include <string> using namespace std; int main() { string input; while(getline(cin, input)) { string tmp = input; int k = 0; int length = input.length(); //冒泡排序 for (int i=0; i<length;i++) { if(input[i]-'a' >= 0 && input[i]-'z' <= 0) { tmp[k] = input[i]; k++; } else if(input[i]-'A' >= 0 && input[i]-'Z' <= 0) { tmp[k] = input[i]; k++; } } for(int i=0; i<k;i++) { for(int j=0; j<k-i-1; j++) { //判断类型 if(tmp[j]-'a' >= 0 && tmp[j]-'z' <= 0) { if(tmp[j+1]-'a' >= 0 && tmp[j+1]-'z' <= 0) { if(tmp[j]>tmp[j+1]) { swap(tmp[j], tmp[j+1]); } } else if(tmp[j+1]-'A' >= 0 && tmp[j+1]-'Z' <= 0) { if(tmp[j]>tmp[j+1]+32) { swap(tmp[j], tmp[j+1]); } } } else if(tmp[j]-'A' >= 0 && tmp[j]-'Z' <= 0) { if(tmp[j+1]-'a' >= 0 && tmp[j+1]-'z' <= 0) { if(tmp[j]>tmp[j+1]-32) { swap(tmp[j], tmp[j+1]); } } else if(tmp[j+1]-'A' >= 0 && tmp[j+1]-'Z' <= 0) { if(tmp[j]>tmp[j+1]) { swap(tmp[j], tmp[j+1]); } } } } } k = 0; for (int i=0; i<length;i++) { if(input[i]-'a' >= 0 && input[i]-'z' <= 0) { cout<<tmp[k]; k++; } else if(input[i]-'A' >= 0 && input[i]-'Z' <= 0) { cout<<tmp[k]; k++; } else { cout<<input[i]; } } cout<<endl; } return 0; }