ZCMU----1779
来源:互联网 发布:淘宝网下载电脑版 编辑:程序博客网 时间:2024/04/29 20:43
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
2111 2 18 3 3 19 2 3 6 5 461 2 3 4 5 6
Sample Output
Case #1: 1 2 18 3 19 6 5 4Case #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;}
0 0
- ZCMU----1779
- ZCMU—1779
- ZCMU-校赛D-1779
- zcmu-1657
- zcmu-1685
- ZCMU----1776
- ZCMU----1777
- ZCMU----1778
- ZCMU-1731
- ZCMU-1658
- ZCMU-1654
- ZCMU-1132
- ZCMU-1037
- ZCMU-1415
- ZCMU-1036
- ZCMU-1723
- ZCMU-1608
- ZCMU-1478
- Android 笔记:Android将图像转换成流存储与将流转换成图像
- 机器学习相关资料
- Visual Studio 2015 设置TortoiseGit
- 电子老鼠闯迷宫
- B-树与B+树的实现及应用
- ZCMU----1779
- 磁盘和磁盘分区表-私房菜学习笔记
- java学习笔记——毕向东视频day01
- SpringMVC常用配置解析与注解
- Android开发05_MediaPlayer之音频播放
- C/C++-技巧-宏
- 回归学习--“理解”数据
- 1.1 起步 - 关于版本控制
- c#属性的赋值问题:为什么属性只读,但是构造函数却还是可以赋值