T1075 明明的随机数 codevs

 空间限制: 128000 KB
 题目等级 : 白银 Silver
题目描述 Description

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

输入描述 Input Description

有2行,第1行为1个正整数,表示所生成的随机数的N个数:

第2行有N个用空格隔开的正整数,为所产生的随机数

输出描述 Output Description

第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小

到大排好序的不相同的随机数。

样例输入 Sample Input

10

20 40 32 67 40 20 89 300 400 15

样例输出 Sample Output

8

15 20 32 40 67 89 300 400

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 int n,a[1105],cnt;
 8 int ans[1105];
 9 bool ok[1105];
10 
11 int main()
12 {
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++)
15     {
16         scanf("%d",&a[i]);
17         if(!ok[a[i]])
18         {
19             cnt++;
20             ok[a[i]]=1;
21             ans[cnt]=a[i];
22         }
23     }
24     printf("%d
",cnt);
25     sort(ans+1,ans+cnt+1);
26     for(int i=1;i<=cnt;i++)
27         printf("%d ",ans[i]);
28             
29     return 0;
30 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/6648291.html