HDU 2000 ASCII码排序

Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
 

 

Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
 

 

Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
 

 

Sample Input
qwe
asd
zxc
 
Sample Output
e q w
a d s
c x z
 
 
分析:注意输入输出格式,排序用冒泡排序就可以通过。
 
AC源代码(C语言):
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>

int main()
{
    char c[3];
    char temp;
    while(scanf("%s", c) != EOF){
        for(int i=0; i<3; i++){
            for(int j=0; j<2-i; j++){
                if(c[j]>c[j+1]){
                    temp = c[j];
                    c[j] = c[j+1];
                    c[j+1] = temp;
                }
            }
        }
        for(int i=0;i<2;i++){
            printf("%c ",c[i]);
        }
        printf("%c\n", c[2]);
    }
    return 0;
}

Java语言:

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

public class Main{
    public static void main(String[] args) {
        Scanner sin=new Scanner(System.in);
        String str = null;
        while(sin.hasNext()){
            str=sin.next();
            char cs[] = str.toCharArray();
            Arrays.sort(cs);
            for(int i=0; i < 2;i++){
                System.out.print(cs[i]+" ");
            }
            System.out.println(cs[2]);
        }
    }
    
}

 

posted @ 2017-05-08 18:21  世界和“你”  阅读(82)  评论(0编辑  收藏  举报