文章

60

粉丝

361

获赞

41

访问

497.5k

头像
简洁
P1255 北京大学机试题
发布于2021年1月13日 10:28
阅读数 10.9k

先将字母提取出来,然后对字母进行排序,在将排序后的字符串写回原字符串

#include <iostream>
#include <string>
#include <string.h>
#include<algorithm>
using namespace std;
struct zifu
{
	char zi;
	int id;
}str[500];
bool compare(zifu a,zifu b)
{
	if(tolower(a.zi)==tolower(b.zi))
		return a.id<b.id;
	else
		return tolower(a.zi)<tolower(b.zi);
}

int main()
{
	string s;
	while(getline(cin,s))
	{
		string s1="";
		int len=s.size();
		int cnt=0;
		for(int i=0;i<len;i++)
			if(s[i]>='A'&&s[i]<='z')//是字母,提取到结构数组中
			{
				str[cnt].zi=s[i];
				str[cnt].id =cnt;
				cnt++;
			}
		sort(str,str+cnt,compare);//对字母排序
        //写回原字符串
		int j=0;
		for(int i=0;i<len;i++)
			if(s[i]>='A'&&s[i]<='z')//是字母 写回
				s[i]=str[j++].zi ;

		cout<<s<<endl;
	}
	//system("pause");
	return 0;
}

 

登录查看完整内容


登录后发布评论

1 条评论
yikeke
2023年4月6日 15:46

赞(0)