G - 不重复数字

给出N个数,要求把其中重复的去掉,只保留第一次出现的数。

例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。

Input

输入第一行为正整数T,表示有T组数据。

接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。

Output

对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。

Sample Input

2 11 1 2 18 3 3 19 2 3 6 5 4 6 1 2 3 4 5 6

Sample Output

1 2 18 3 19 6 5 4 1 2 3 4 5 6

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;    

int main()             //此题消除重复的数,用set输出时数据一个一个输出,并且有空格,最后有换行,一般先将 
{
    int n,m,k,i;       //先将第一个输出,例 16 19 23行。 
    set<int>a;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        scanf("%d",&k);
        printf("%d",k);
        a.clear();
        a.insert(k);
        for( i=2;i<=m;i++)
        {
            scanf("%d",&k);
            if(a.count(k)==0)
            printf(" %d",k);
            a.insert(k);    
        }
        puts("");
    }
}

猜你喜欢

转载自blog.csdn.net/lbs311709000127/article/details/81148740
今日推荐