ASCII码排序 --JAVA

题目:

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

Input

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

Output

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

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z

代码如下:

JAVA:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	while(input.hasNext()) {
	String a;
	a=input.nextLine();
	char aa,bb,cc,tt;
	aa=a.charAt(0);
	bb=a.charAt(1);
	cc=a.charAt(2);
	if(aa>bb) {
		tt=aa;
		aa=bb;
		bb=tt;
	}
	if(aa>cc) {
		tt=aa;
		aa=cc;
		cc=tt;
	}
	if(bb>cc) {
		tt=bb;
		bb=cc;
		cc=tt;
	}
	System.out.println(aa+" "+bb+" "+cc);
	}
}
}

C++:

#include<stdio.h>
int main()
{
    char x,y,z,t;
    while(~scanf("%c%c%c",&x,&y,&z))
    {
        getchar();
        if(x>y)
        {
            t=x;
            x=y;
            y=t;
        }
        if(x>z)
        {
            t=x;
            x=z;
            z=t;
        }
        if(y>z)
        {
            t=z;
            z=y;
            y=t;
        }
        printf("%c %c %c\n",x,y,z);
    }
    return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;

char a[4];

int main()
{
    while(~scanf("%s",a))
    {
        sort(a,a+3);
        printf("%c %c %c\n",a[0],a[1],a[2]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/83660902