HDU 2000 ASCIIコードソート(C,Javaの2つのバージョン)

2178 ワード

ASCIIコードソート
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 129693    Accepted Submission(s): 53459
Problem Description
3文字入力後、各文字のASCIIコードの小さい順に出力します.
 
Input
入力データには複数のグループがあり、各グループは1行を占め、3文字で構成され、スペースはありません.
 
Output
各入力データのセットについて、1行出力し、文字の間にスペースを1つずつ分けます.
 
Sample Input

   
   
   
   
qwe asd zxc

 
Sample Output

   
   
   
   
e q w a d s c x z

 
Author
lcy
 
Source
C言語プログラミング練習(一) 
C言語ACコード:
#include <stdio.h>
int main()
{
    char a,b,c;
    while(scanf("%c%c%c",&a,&b,&c)!=EOF)
    {
        getchar();//       
        if (a<b)
        {
            if (c<a) printf("%c %c %c
",c,a,b); else if (b<c) printf("%c %c %c
",a,b,c); else printf("%c %c %c
",a,c,b); } else { if (c<b) printf("%c %c %c
",c,b,a); else if (c>a) printf("%c %c %c
",b,a,c); else printf("%c %c %c
",b,c,a); } } return 0; }

Java ACコード:
JavaもCのようにいちいち比較できますが、Javaにはsort()関数があり、サボる!
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			String string = sc.nextLine();<span style="font-family: Arial, Helvetica, sans-serif;">//       </span>
			char str[] = new char[3];

			for (int i = 0; i < str.length; i++) {
				str[i] = string.charAt(i);
			}

			Arrays.sort(str);
			for (int i = 0; i < str.length; i++) {
				if(i!=0){
					System.out.print(" ");
				}
				System.out.print(str[i]);
			}
			System.out.println();
			
		}
	}

}