题解 | 字符串内排序
字符串内排序
https://www.nowcoder.com/practice/cc2291ab56ee4e919efef2f4d2473bac
#include <stdio.h> #include <string.h> #define str_len 201 int merge(char str[str_len], int start, int mid, int end){ char result[str_len]; int k=0; int i=start; int j=mid+1; while(i<=mid && j<=end){ if(str[i]>str[j]){ result[k++] = str[j++]; }else{ result[k++] = str[i++]; } } // 解决剩余的 if(i<=mid){ while(i<=mid){ result[k++] = str[i++]; } } if(j<=mid){ while(j<=end){ result[k++] = str[j++]; } } // 结果 for(i=start,j=0; j<k; i++,j++){ str[i] = result[j]; } return 0; } int mergeSort(char str[str_len], int start, int end){ if(start >= end){ return 0; } int mid = (start+end)/2; mergeSort(str, start, mid); mergeSort(str, mid+1, end); merge(str, start, mid, end); return 0; } int main() { char str[str_len]; while(scanf("%s",str) != EOF){ int n = strlen(str); // 排序 mergeSort(str, 0, n-1); // 输出 printf("%s\n", str); } return 0; }