#include <stdio.h> #include <stdlib.h> #include<string.h> //思路:先将小写字母全部化为大写,并对变化的元素设置标记,再进行稳定的排序,对于非字母直接跳过排序 //最后根据标记将变化的大写字母还原为小写字母 char a[1000]; char change[1000] = {0}; int main() { gets(a); int n = strlen(a); for (int i = 0; i < n; i++) { //小写化为大写 if (a[i] >= 97 && a[i] <= 122) { a[i] = a[i] - 32; change[i] = 1; } } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { int tmp = j - 1; if (a[j] < 65 || a[j] > 90) { continue; } while (a[tmp] < 65 || a[tmp] > 90 && tmp >= 0) { //注意:必须加tmp>=0的判断,不然就数组越界了,也就是oj报错的原因 tmp--; } if (tmp < 0) { continue; } if (a[tmp] > a[j]) { int tmp2 = change[tmp]; char tmp1 = a[tmp]; change[tmp] = change[j]; change[j] = tmp2; a[tmp] = a[j]; a[j] = tmp1; } } } for (int i = 0; i < n; i++) { if (change[i] == 1) { a[i] = a[i] + 32; } } printf("%s", a); }