zcmu--1779: 无法言表(set容器)

1779: 无法言表

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 310  Solved: 117
[Submit][Status][Web Board]

Description

给出N个数,要求把其中的重复的去掉,只保留第一次出现的数.1 <= N <= 50000,给出的数在32位有符号整数范围内。

Input

第一行T(T<=10),接下来一个数n,接下来n个数

Output

Case #x: y1,y2,...,x是测试编号从1开始,y_i表示答案

Sample Input

2

11

1 2 18 3 3 19 2 3 6 5 4

6

1 2 3 4 5 6

Sample Output

Case #1: 1 2 18 3 19 6 5 4

Case #2: 1 2 3 4 5 6

【分析】一开始想用普通循环做下去,到没有超时而是WA,,想不通,正好今天新学习了set就拿来试着用下咯~很方便

set讲解戳这里!

因为set存的值不会有重复的,所以,存一个判断一个输出一个就很方便了;

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x,t,n,k=1;
    set<int> s;
    scanf("%d",&t);
    while(t--)
    {
        s.clear();//清空容器
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(!i)
            {
                s.insert(x);//插入数据函数
                printf("Case #%d: %d",k++,x);
             }
            else
            {
                if(s.count(x)==0)//该元素在容器中不曾出现过,就可以将该数输出并插入到容器中
                {
                    s.insert(x);
                    printf(" %d",x);
                }
            }
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81813364
今日推荐