HDU-2000ASCII码排序

来源:互联网 发布:图片写字软件 编辑:程序博客网 时间:2024/06/07 02:43

ASCII码排序


 

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

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

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

Sample Input
qweasdzxc
 

Sample Output
e q wa d sc x z
 
 
这道题也是比较容易的,只需要用String接收,然后转换成字符数组,然后进行排序即可。
<span style="font-family:Times New Roman;font-size:14px;">import java.util.Scanner;public class P2000 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);while(sc.hasNext()){   //输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。//这里我用字符串进行接收String str=sc.nextLine();char a[]=new char[3];//字符串再转换成字符数组,然后进行排序,a=str.toCharArray();for(int i=0;i<2;i++){for(int j=i+1;j<3;j++){if(a[i]>a[j]){char temp=a[i];a[i]=a[j];a[j]=temp;}}}print(a);}}private static void print(char[] a) {for(int i=0;i<a.length;i++){if(i==0){System.out.print(a[i]);}else{System.out.print(" "+a[i]);}}System.out.println();}}</span>

0 0
原创粉丝点击