1564 - 字符排序

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

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

题目输入

输入第一行为样例数m,接下来m行每行一个字符串,字符串长度不超过20。

题目输出

输出m行表示排序完的字符串。

输入/输出样例

输入格式

1
dcab

输出格式

abcd

C语言解答

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
  int m,n,i,j,k;
  char s[30];
  scanf("%d",&m);
  while(m--){
    scanf("%s",s);
    n=strlen(s);
    //printf("%d\n",n);
    for(i=0;i<n;i++){
      k=i;
      for(j=i;j<n;j++)
        if(s[j]<s[k])k=j;
      putchar(s[k]);
      //printf("  %d   ",k);
      s[k]=s[i];
    }
    putchar('\n');
  }
}

C++解答

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;

int main()
{
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin>>s;
        sort(s.begin(),s.end());
        cout<<s<<endl;
    }
    return 0;
}

Java解答



import java.util.Arrays;
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
	  Scanner s = new Scanner(System.in) ;
	  
	  int n = s.nextInt() ;
	  
	  for (int i = 0; i < n; i++) {
		 String str = s.next() ;
		 
		 char[]c = str.toCharArray() ;
		 
		 Arrays.sort(c) ;
		 
		 for (int j = 0; j < c.length; j++) {
		    System.out.print(c[j]) ;	
		 }
		 System.out.println();
	  }
   }
}