2761: [JLOI2011]不重复数字
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5396 Solved: 2039
[ Submit][ Status][ Discuss]
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
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
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来进行输入输出操作,以免浪费不必要的时间。
问题链接:HYSBZ2761 不重复数字
问题简述:(参见上文)
问题分析:这是一个简单的去重复问题,查找即可。数据量不是很大,用顺序查找。
C语言实现时,数组a[]中,存放已经出现过的数。
C++的STL实现时,可以使用集合set实现排除重复,也可以使用map实现排除重复。
AC的C语言程序如下:
/* HYSBZ2761 不重复数字 */
#include <stdio.h>
#define N 50000
int a[N], acount;
int find(int v)
{
int i;
for(i=0; i<acount; i++)
if(v == a[i])
return 1;
return i != acount;
}
int main(void)
{
int t, n, v, outcount;
scanf("%d", &t);
while(t--) {
acount = 0;
outcount = 0;
scanf("%d", &n);
while(n--) {
scanf("%d", &v);
if(!find(v)) {
a[acount++] = v;
if(outcount++ != 0)
printf(" ");
printf("%d", v);
}
}
printf("\n");
}
return 0;
}
AC的C++语言程序如下:
/* HYSBZ2761 不重复数字 */
#include <iostream>
#include <set>
#include <stdio.h>
using namespace std;
set<int> s;
int main()
{
int t, n, v, outcount;
scanf("%d", &t);
while(t--) {
s.clear();
outcount = 0;
scanf("%d", &n);
while(n--) {
scanf("%d", &v);
set<int>::iterator iter = s.find(v);
if(iter == s.end()) {
s.insert(v);
if(outcount++ != 0)
printf(" ");
printf("%d", v);
}
}
printf("\n");
}
return 0;
}