文章

34

粉丝

0

获赞

345

访问

6.5k

头像
字符串排序2 题解:
P1255 北京大学机试题
发布于2026年3月8日 22:15
阅读数 234

 

#include <bits/stdc++.h>
using namespace std;

bool cmp(char a,char b){
	if(a>='A'&&a<='Z') a=a+32;
	if(b>='A'&&b<='Z') b=b+32;
	return a<b;
}
char s[1005];
int main(){
	while(gets(s)!=0){
		char abc[1005];
		int arc=0;
		int len_s=strlen(s);
		for(int i=0;i<len_s;i++){
			if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
				abc[arc++]=s[i];
			}
		}
		stable_sort(abc,abc+arc,cmp);
		int cra=0;
		for(int i=0;i<len_s;i++){
			if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
				printf("%c",abc[cra++]);
			}
			else printf("%c",s[i]);
		}
		printf("\n");
	}
	return 0; 
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发