BZOJ 2761 不重复数字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39645344/article/details/83501112

Description

给出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
Hint

对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;


对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;


对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。


提示:


由于数据量很大,使用C++的同学请使用scanf和printf来进行输入输出操作,以免浪费不必要的时间。

set

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		set<int>s;
		int n;
		scanf("%d",&n);
		for(int i = 1;i<=n;i++)
		{
             int temp;
			scanf("%d",&temp);
			if(!s.count(temp))
			{
				s.insert(temp);
				if(i==1)
					printf("%d",temp);
				else
					printf(" %d",temp);
			}
		}
		printf("\n");
	}
}

其中 s.count(x) 只有`01

count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。

#include <iostream>  
#include <set>  
  
using namespace std;  
  
int main()  
{  
    set<int> s;  
    s.insert(1);  
    s.insert(2);  
    s.insert(3);  
    s.insert(1);  
    cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl;  
    cout<<"set 中 4 出现的次数是 :"<<s.count(4)<<endl;  
    return 0;  
}  

map

#include <bits/stdc++.h>
#include <algorithm>

using namespace std;
map<int ,int > map1[199];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){

        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {

            int x;
            scanf("%d",&x);
            if(map1[t][x])
            {
                continue;
            }
            map1[t][x]=1;
            printf("%d ",x);
        }
        printf("\n");
    }
    return 0;

猜你喜欢

转载自blog.csdn.net/weixin_39645344/article/details/83501112
今日推荐