题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/d9aa3894d3aa4887843a85d26daa4437

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

bool cmp(char a,char b)
{
	//大写化为小写,再比较,满足题目所要求的不区分大小写 
	if(a>='A'&&a<='Z') a=a-'A'+'a';
	if(b>='A'&&b<='Z') b=b-'A'+'a';
	return a<b;
}

bool isWord(char a)
{
	if((a>='a'&&a<='z')||(a>='A'&&a<='Z'))	return true;
	else	return false;
}
int main(){
	string s;
	while(getline(cin,s))
	{
		string t;
		for(int i=0;i<s.size();i++)
		{
			if(isWord(s[i]))//是英文字母 
			{
				t+=s[i];
			}
		}
		stable_sort(t.begin(),t.end(),cmp);//要求稳定排序 
		for(int i=0;i<s.size();i++)
		{
			if(!isWord(s[i]))
			{
				t.insert(t.begin()+i,s[i]);
			}
		}
		cout<<t<<endl;
	}
	return 0;
}

#计算机考研复试jishi#
全部评论

相关推荐

迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务