天天看點

HDU2000ASCII碼排序(C,Java兩個版本)ASCII碼排序

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 輸入三個字元後,按各字元的ASCII碼從小到大的順序輸出這三個字元。  

Input 輸入資料有多組,每組占一行,有三個字元組成,之間無空格。  

Output 對于每組輸入資料,輸出一行,字元中間用一個空格分開。  

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\n",c,a,b);
            else if (b<c) printf("%c %c %c\n",a,b,c);
            else printf("%c %c %c\n",a,c,b);
        }
        else
        {
            if (c<b) printf("%c %c %c\n",c,b,a);
            else if (c>a) printf("%c %c %c\n",b,a,c);
            else printf("%c %c %c\n",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();
			
		}
	}

}