字符过滤、排序
内存限制:256 MiB 时间限制:1000 ms 标准输入输出
输入一行文本,包含大、小字母、数字、空格等。要求过滤掉除字母外的其他字符,并将所有的大写字母转换成小写字母,然后将字母从小到大排序,输出结果。
输入格式
第1行:一行文本,长度小于1000
输出格式
第1行:处理后的一行文本
样例输入
She sells sea shells
样例输出
aeeeehhllllssssss
#include <bits/stdc++.h>
using namespace std;
string s;
int a[100000];
int ans;
int main() {
int count=0;
getline(cin,s);
ans=s.length();
for(int i=0;i<ans;i++){
if(s[i]!=' '){
a[count]=int(s[i]);
count++;
}
}
for(int i=0;i<count;i++){
if(a[i]>=65&&a[i]<=90){
a[i]+=32;
}
}
for(int i=0;i<count-1;i++){
for(int j=0;j<count-i-1;j++){
if(a[j+1]<a[j]){
swap(a[j],a[j+1]);
}
}
}
for(int i=0;i<count;i++){
cout<<char(a[i]);
}
return 0;
}
这边建议用桶排
桶排代码简单又好理解
#include<bits/stdc++.h>
using namespace std;
int a[256];
int main()
{
char c[114514];
gets(c);
int len=strlen(c);
for(int i=0;i<len;i++)
{
if(c[i]>='A'&&c[i]<='Z') c[i]=c[i]+32;
a[c[i]]++;
}
for(int i='a';i<='z';i++)
{
for(int j=1;j<=a[i];j++)
{
cout<<char(i);
}
}
return 0;
}