HDoj 2000 ASCII码排序

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

 

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

 

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

 

Sample Input
qwe asd zxc
 

 

Sample Output
e q w a d s c x z
 

 

Author
lcy
 

 

Source
 
 
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  2001 2002 2004 2003 2005 
 
注意:
1在进行两个字母交换的时候,一开始不能是a=temp;而是temp=a,因为一开始tempp是未知变量必须先赋值
2在输入数据qwe asd zxc时注意不要使用scanf,因为scanf会把空格键也当作输入字符,这样在控制台上的输出就会不正确,所以我的方法是改用cin,cin遇到空格时就会终止这次输入,进行新的输入
 
C++代码如下:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
    char a,b,c,temp;
    while(cin>>a>>b>>c)
    {
        if(a>b)
        {
            temp=a;
            a=b;
            b=temp;
        }
        if(a>c)
        {
            temp=c;
            c=a;
            a=temp;
        }
        if(b>c)
        {
            temp=c;
            c=b;
            b=temp;
        }
        printf("%c %c %c\n",a,b,c);
    }
}

 

后来发现纯C语言,后面加个空格吸收符也是可行的

#include<stdio.h>
int main()
{
    char a,b,c,temp;
    while(scanf("%c%c%c",&a,&b,&c)!=EOF)
    {
        if(a>b)
        {
            temp=a;
            a=b;
            b=temp;
        }
        if(a>c)
        {
            temp=c;
            c=a;
            a=temp;
        }
        if(b>c)
        {
            temp=c;
            c=b;
            b=temp;
        }
        printf("%c %c %c\n",a,b,c);
        getchar();
    }
}

 

posted on 2020-03-10 14:49  沈香茶  阅读(147)  评论(0)    收藏  举报