1779: 无法言表
Time Limit: 1 Sec Memory Limit: 128 MB[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
【分析】
删除重复的数字(只保留第一次出现的数字),题面很简单,但有坑,用数组标记不行
【代码】
用map写的,方便一些,下面有用结构写的,较烦
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <algorithm> using namespace std; const int maxn = 1e5+10; map<int,int>a; int b[maxn]; int main() { int t; scanf("%d",&t); int cas = 1; while(t--){ a.clear(); memset(b,0,sizeof b); int n; int j=0; scanf("%d",&n); for(int i=0;i<n;i++){ int x; scanf("%d",&x); if(a[x] == 0){ b[j++] = x; a[x] = 1; } } printf("Case #%d:",cas++); for(int i=0;i<j;i++){ printf(" %d",b[i]); } puts(""); } return 0; }
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
struct f
{
int x; ///数字
int vis; ///标记是否出现过
int num; ///数字位置
}a[maxn];
int cmp1(f n,f m)
{
return n.num<m.num;
}
int cmp2(f n,f m)
{
if(n.x == m.x)
return n.num<m.num;
return n.x<m.x;
}
int b[maxn];
int main()
{
int t;
scanf("%d",&t);
int cas = 1;
while(t--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i].x);
a[i].num = i;
a[i].vis = 0;
}
sort(a,a+n,cmp2);
int tmp = a[0].x;
for(int i=1;i<n;i++){
if(tmp == a[i].x){
a[i].vis = 1;
}
else{
tmp = a[i].x;
}
}
sort(a,a+n,cmp1);
printf("Case #%d:",cas++);
for(int i=0;i<n;i++){
if(!a[i].vis)
printf(" %d",a[i].x);
}
puts("");
}
return 0;
}