这个题的排序相比之前的会有点麻烦,第一个是需要字母从小到大,第二个字母相同位置不变,第三个是其他字符位置不动,那么其实这里的话可以使用一个桶排序,先将这写字母按照顺序放好,判断当前字符串的位置将其放进去即可。其实应该也算是稳定排序的一种,后面可以试下能不能用冒泡排序去做
#include<stdio.h>
#include<string.h>
char str[1005];
struct postion{
char ch[105];
int length;
}pos[27];
int main(){
while(fgets(str,1005,stdin)!=NULL){
for(int i=0;i<27;i++){
memset(pos[i].ch,0,sizeof(pos[i].ch));
pos[i].length=0;
}
//每个字母遍历放在桶里面
for(int i=0;str[i]!=0;i++){
if(str[i]>='a'&&str[i]<='z'){
pos[str[i]-'a'].ch[pos[str[i]-'a'].length++]=str[i];
str[i]='a';
}else if(str[i]>='A'&&str[i]<='Z'){
pos[str[i]-'A'].ch[pos[str[i]-'A'].length++]=str[i];
str[i]='a';
}
}
int x=0,y=0;
//处理桶的数据,放在str里面
for(int i=0;str[i]!=0;){
if(str[i]=='a'){
while(pos[x].length==0){
x++;
}
str[i]=pos[x].ch[y];
y++;
i++;
if(y==pos[x].length){
y=0;
pos[x].length=0;
}
}else{
i++;
}
}
fputs(str,stdout);
}
return 0;
}
京公网安备 11010502036488号