首页 > 试题广场 >

字符串内排序

[编程题]字符串内排序
  • 热度指数:15233 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。

输入描述:
测试数据有多组,输入字符串。


输出描述:
对于每组输入,输出处理后的结果。
示例1

输入

bacd

输出

abcd
直通BAT优惠码: AiOcnz2
发表于 2017-04-24 18:05:23 回复(0)
#include <iostream>
#include <map>

using namespace std;

int main() {
    string str;
    cin >> str;
    map<char, int> dict;
    for (char c:str) {
        auto it = dict.find(c);
        if (it != dict.end()) {
            dict[c]++;
        } else {
            dict[c] = 1;
        }
    }
//    重新排列字母,默认就是a-z排列
    string res;
    for (auto entry:dict) {
        int i = entry.second;
        while (i-- > 0) {
            res.push_back(entry.first);
        }
    }
    cout << res << endl;
}

编辑于 2021-01-08 17:06:00 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[201]={};
    while(scanf("%s ",a)!=EOF){
        sort(a,a+strlen(a));
        puts(a);
    }
}
发表于 2019-03-04 13:57:43 回复(0)

python only one line:


while True:
    try:
        print("".join(sorted(input())))
    except:
        break
发表于 2017-10-01 10:38:59 回复(2)
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    string s;
    while(cin>>s){
        sort(s.begin(),s.end());
        cout<<s<<endl;
    }
    return 0;
}

发表于 2016-10-29 10:11:19 回复(1)
#include<stdio.h>//字符排序直接按照整数排序即可
int main()
{
    char a[201],t;
    scanf("%s",a);
    for(int i=0;i<strlen(a)-1;i++)//冒泡排序
        for(int j=0;j<strlen(a)-1-i;j++)
            if(a[j]>a[j+1])
            {//交换
                t=a[j];a[j]=a[j+1];a[j+1]=t;
            }
    printf("%s",a);
}

发表于 2020-04-08 15:08:45 回复(0)
import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        char[] ch = new Scanner(System.in).nextLine().toCharArray();
        Arrays.sort(ch);
        System.out.print(ch);
    }
}

发表于 2018-08-03 03:51:05 回复(0)
import java.util.Scanner;
import java.util.Arrays;
public class Main{
	public static void main(String args[]){
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String str=sc.nextLine();
			char array[]=str.toCharArray();
			Arrays.sort(array);
			for(int i=0;i<array.length;i++)
				System.out.print(array[i]);
			System.out.println();
			
			
		}
		sc.close();
	}
}

发表于 2016-05-20 11:52:32 回复(0)
#include <stdio.h>
#include <string.h>
typedef int Position;
// 快排
Position partion(char *str, int low, int high)
{
  char pivot = str[low];
  while (low < high)
  {
    while (low < high && str[high] >= pivot)
      high--;
    str[low] = str[high];
    while (low < high && str[low] <= pivot)
      low++;
    str[high] = str[low];
  }
  str[low] = pivot;
  return low;
}
void QuickSort(char *str, int low, int high)
{
  if (low < high)
  {
    Position pivotProps = partion(str, low, high);
    QuickSort(str, low, pivotProps - 1);
    QuickSort(str, pivotProps + 1, high);
  }
}
int main()
{
  char str[200];
  while (scanf("%s", str) != EOF)
  { // 注意 while 处理多个 case
    QuickSort(str, 0, strlen(str) - 1);
    printf("%s", str);
  }
  return 0;
}

发表于 2022-03-21 19:41:39 回复(0)
Java解法
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] array = scanner.nextLine().toCharArray();
        Arrays.sort(array);
        for (char c : array) {
            System.out.print(c);
        }
    }
}


发表于 2020-03-06 16:31:30 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    char ch[201];
    while(cin>>ch){
        sort(ch,ch+strlen(ch));
        cout<<ch<<endl;
    }
}

发表于 2020-01-13 16:32:08 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int c(const void*a,const void*b){return *(char*)a-*(char*)b;}
int main (){//the shorter,the better.
    int n;char s[200];
    for(;~scanf("%s",s)&&(qsort(s,strlen(s),sizeof(char),c),printf("%s\n",s)););
}

发表于 2018-01-14 16:24:55 回复(1)
#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(char a,char b){
    return a < b;
}

int main() {
    string str;
    cin >> str;

    int len = str.length();
    char* s = new char[len];
    for(int i = 0;i < len; i++){
        s[i] = str[i];
    }

    sort(s, s + len, cmp);

    puts(s);

}

发表于 2023-01-16 22:16:05 回复(0)
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    while (cin >> str) {
        sort(str.begin(), str.end());
        cout << str << endl;
    }
    return 0;
}

发表于 2024-03-04 09:34:15 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main() {
    string num;
    while(cin>>num)
    {
        int len=num.size();
        sort(&num[0],&num[len-1]+1);
        cout<<num<<endl;

    }
}


编辑于 2024-02-29 17:09:23 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<math.h>

void kuaipai(char a[],int h,int r)
{
    char c = a[h];
    int f = h, e = r;
    while (h < r)
    {
        while (h<r&&a[r] >= c)
            r--;
        a[h] = a[r];
        while (h<r&&a[h] < c)
            h++;
        a[r] = a[h];
    }
    a[h] = c;
    if (f < e)
    {
        kuaipai(a, f, h - 1);
        kuaipai(a, h + 1, e);
    }
}

int main()
{
    char s[200];
    while (scanf("%s", s) != EOF)
    {
        int n = strlen(s);
        kuaipai(s, 0, n-1);
        printf("%s\n", s);
    }
}
编辑于 2024-02-04 11:14:37 回复(0)
console.log((await readline()).split('').sort().join(''))
发表于 2023-08-27 20:40:53 回复(0)
偷懒直接用 sort 了。
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
    string in;
    while (cin >> in) { 
        sort(in.begin(), in.end());
        cout << in << endl;
    }
}


发表于 2023-03-21 15:47:45 回复(0)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    char arr[201];
    while (scanf("%s", arr) != EOF) {
        string str = arr;
        int length = str.length();
        sort(arr, arr + length);
        printf("%s", arr);
    }
}


发表于 2023-03-17 21:02:57 回复(0)
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main() {
    string str;
    while(cin>>str){
        sort(str.begin(),str.end());
        cout<<str<<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2023-03-14 23:13:15 回复(0)