13002: 【原3002】Ruko Sort
题目
题目描述
author: sonicmisora 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/3002
Description
在地球上坠机的露子为了返回她的母星——大熊星座第47星系第3小行星,正在努力接受宇宙电波。 有一天,她突然收到了一系列数字,这些数字共有n个,而且杂乱无序。 为了找出这些数字的规律,她决定将它们去重(即相同的数只保留一个)后,再从小到大排序。 为了露子能重返家园,现在就请你来帮帮露子吧。
保证n<=100,且对于数列中任意一个数Ai(1<=i<=n), 有1 <= Ai <= 1000。
Input Format
输入包括两行。 第一行为一个整数n,代表数列的长度。 (1<=n<=100) 第二行为n个整数,第i个数即是数列的第i个数Ai。(1<=Ai<=1000)
Output Format
输出也是两行。 第一行为一个整数m,代表去重并排序后新数列的长度。 第二行为m个整数,第i个数代表新数列的第i个数。
Sample Input
10
20 40 32 67 40 20 89 300 400 15
Sample Output
8
15 20 32 40 67 89 300 400
ligongzzz's solution
#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;
bool val_num[1009] = { 0 };
int main() {
int n;
scanf("%d", &n);
int cnt = 0;
for (int i = 0; i < n; ++i) {
int temp;
scanf("%d", &temp);
if (!val_num[temp]) {
++cnt;
val_num[temp] = true;
}
}
printf("%d\n", cnt);
for (int i = 0; i < 1005; ++i) {
if (val_num[i])
printf("%d ", i);
}
return 0;
}
yyong119's solution
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000;
int main() {
int n, temp, num = 0;
int a[MAXN + 1];
memset(a, 0, sizeof(a));
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> temp;
if (a[temp] == 0) {
++num;
++a[temp];
}
}
cout << num << endl;
for (int i = 1; i <= MAXN; ++i) if (a[i]) cout << i << " ";
}