字符串排序【北京大学考研机试题】

字符串排序

输入一个长度不超过 20 的字符串,对所输入的字符串,按照 ASCII 码的大小从小到大进行排序,请输出排序后的结果。

输入格式
一行,一个字符串。

输出格式
一行,排序后的字符串。

数据范围
输入字符串长度不超过 20。

输入样例:
dcba
输出样例:
abcd

代码

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 1e6+10;
const int M = 2e5+10;
int n,m;
const int maxn = 1 << 31 - 1;

void solve(){
	string s;
	cin >> s;
	sort(s.begin(),s.end());
	cout << s;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}

小结

  1. 字符本身就是数字,排序根据ASCII码排序
  2. sort可以直接给stringyong
posted @ 2023-03-06 15:13  Keith-  阅读(18)  评论(0编辑  收藏  举报