题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h> #include <string.h> char str2pos(char s) { if(s >= 'a' && s <= 'z') { return s - 'a'; } else if(s >= 'A' && s <= 'Z') { return s - 'A'; } else { return (char)55; } } int main() { char s[1000] = {0}; int tmp[4] = {0}, min = 0; while (gets(s) != NULL) { int cnt = strlen(s); for(int i = 0; i < cnt - 1; i++) { tmp[2] = 0; tmp[0] = str2pos(s[i]); if(tmp[0] == 55) { continue; } for(int j = i + 1; j < cnt; j++) { tmp[1] = str2pos(s[j]); if(tmp[1] == 55) { continue; } if(tmp[1] < tmp[0]) { tmp[0] = tmp[1]; tmp[2] = j; } } tmp[3] = s[tmp[2]]; int tmpk = tmp[2]; for(int k = tmp[2]; k > i; k--) { tmp[1] = str2pos(s[k - 1]); if(tmp[1] == 55) { continue; } else { s[tmpk] = s[k - 1]; tmpk = k - 1; } if(k == i + 1)s[i] = tmp[3]; } } printf("%s", s); } return 0; }