(HDOJ 2000)ASCII码排序

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

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

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

Sample Input
qwe 
asd 
zxc
 

Sample Output
e q w 
a d s 
c x z
 

AC code:

#include<stdio.h>
#include
<string.h>
#define N 3
void swap(char *a,char *b)
{
     
char temp;
     temp
=*a;
     
*a=*b;
     
*b=temp;
}

void sort(char a[])
{
     
char *p;
     p
=a; 
     
if(a[0]>a[1])
       swap(p,p
+1);
     
if(a[0]>a[2])
       swap(p,p
+2);
     
if(a[1]>a[2])
       swap(p
+1,p+2);
}

int main()
{
     
int i;
     
char str[5];
     
while(gets(str))
     {
       sort(str);
       
for(i=0; i<3; i++)
       {
         
if(i<2)
           printf(
"%c ",str[i]);
         
else
           printf(
"%c",str[i]);
       }
       printf(
"\n");
     }
     
return 0;

} 

作者:cpoint
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/cpoint/p/2015340.html