1086: ASCII码排序(多实例测试)
题目描述
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
输入
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
输出
对于每组输入数据,输出一行,字符中间用一个空格分开。
样例输入 Copy
qwe
asd
zxc
样例输出 Copy
e q w
a d s
c x z
提示
注意:scanf("%c",&ch)格式读字符时,所有字符都是有效输入,如回车符、空格符等,所以每行读取字符之后要处理最后的回车符。
代码
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch1,ch2,ch3,t;
while(scanf("%c%c%c%*c",&ch1,&ch2,&ch3)!=EOF)//最后一个是吸收回车符
{
if(ch1>ch2)
{
t=ch1;ch1=ch2;ch2=t;
}
if(ch1>ch3)
{
t=ch1;ch1=ch3;ch3=t;
}
if(ch2>ch3)
{
t=ch2;ch2=ch3;ch3=t;
}
printf("%c %c %c\n",ch1,ch2,ch3);
}
return 0;
}
代码
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch1,ch2,ch3,t;
while(scanf("%c%c%c",&ch1,&ch2,&ch3)!=EOF)
{
getchar(); //吸收回车符
if(ch1>ch2)
{
t=ch1;ch1=ch2;ch2=t;
}
if(ch1>ch3)
{
t=ch1;ch1=ch3;ch3=t;
}
if(ch2>ch3)
{
t=ch2;ch2=ch3;ch3=t;
}
printf("%c %c %c\n",ch1,ch2,ch3);
}
return 0;
}