ASCII码排序
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z
思路一
- 选择排序
- getchar 吸收输入中多余的\n
#include<iostream>
using namespace std;
int main()
{
char a[5];
while (scanf("%c%c%c", &a[0], &a[1], &a[2])!=EOF)
{
//排序abc
for (int i = 0; i < 2; i++)
{
int min = i;
for (int j = i + 1; j < 3; j++)
{
if (a[min] > a[j])
{
char t = a[min];
a[min] = a[j];
a[j] = t;
}
}
}
//输出
printf("%c %c %c\n", a[0], a[1], a[2]);
getchar();
}
return 0;
}
思路二
- if else交换保证a<b<c
- 用"%*c"(空字符,不存储字符的字符)滤掉回车
参考https://blog.csdn.net/cjhxydream/article/details/68064205
#include <stdio.h>
int main()
{
char a,b,c,tmp;
while(scanf("%c%c%c%*c",&a,&b,&c)!=EOF) //用"%*c"(空字符,不存储字符的字符)滤掉回车
{
if(a>b) tmp=a,a=b,b=tmp; //逗号表达式,简洁
if(a>c) tmp=a,a=c,c=tmp;
if(b>c) tmp=b,b=c,c=tmp;
printf("%c %c %c\n",a,b,c);
}
return 0;
}