NYOJ ASCII码排序

时间限制:3000 ms  |  内存限制:65535 KB

难度:2

输入

第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。

输出

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

样例输入

2
qwe
asd

样例输出

e q w
a d s

描述

输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。

#include <iostream>
#include <cstdio>
#include <set>

using namespace std;

struct ST{
    char ch;

    /*排序不去重*/
    friend bool operator < (const ST& n1,const ST& n2){
            //当两个元素相同时不去重
			if(n1.ch == n2.ch)return true;
			else return n1.ch < n2.ch;
	}
}e[1000];

int main(int argc, char const *argv[]) {
    //N - 次数
    int N;

    scanf("%d",&N);
    //去除输入N后的 \n
    getchar();

    while(N--){

        set<ST> et;

        for(int i = 0;;i++){
            scanf("%c",&e[i].ch);
            //当符号是\n时结束这次循环
            if(e[i].ch == '\n')break;
            et.insert(e[i]);
        }
        //输出
        for(set<ST>::iterator k = et.begin();k != et.end();k++)
            cout << k->ch << " ";
        //换行
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/WX_1218639030/article/details/84065389