指针 不重复数字 洛谷P4305——简单

题目描述

给出N个数,要求把其中重复的去掉,只保留第一次出现的数。
例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。

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

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

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

样例输入
2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6

样例输出
1 2 18 3 19 6 5 4
1 2 3 4 5 6

程序代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <cstring>
#include <map>
#include <cmath>
using namespace std;
int a[50005],t,n,b;
int Fun(int x)
{
    int y;
    if(b<0) 
	y=-b%50005;
	else 
	y=b%50005;
    while(a[y]&&a[y]!=b)
    {	++y;
		y=y%50005;
	}
    return y;
}
void fun1(int b)
{
    a[Fun(b)]=b;
}
bool fun2(int b)
{
    return a[Fun(b)]==b;
}
int main()
{
    int i,j;
	scanf("%d",&t);
    for(j=0;j<t;j++)
    {
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&b);
            if(!fun2(b)) 
             {
			 	printf("%d ",b);
			 }
			 fun1(b);
        }
        putchar(10);
    }
}

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

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

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

PS:本博客属于中国石油大学胜利学院ACM协会所有!
BY:高文欣

猜你喜欢

转载自blog.csdn.net/weixin_43805821/article/details/84928236
今日推荐