# 342 条题解

• @ 2017-12-02 15:15:09

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n,a[1009]={0},t,s=0;
cin>>n;
for (int i=1;i<=n;++i)
{
cin>>t;
a[t]++;
}
for (int i=1;i<=1000;++i)
{
if (a[i]!=0)
{
++s;
}
}
cout<<s<<endl;
for (int i=1;i<=1000;++i)
{
if (a[i]!=0)
{
cout<<i<<" ";
}
}
cout<<endl;
return 0;
}

• @ 2017-12-28 11:20:41

智慧

• @ 2017-10-09 17:38:05

#include <iostream>
#include <algorithm>
using namespace std;
int a[1010];
int book[1010];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int ans=0;
for(int i=0;i<n;i++)
{
ans++;
book[i]=1;
if(i)
{
if(a[i]==a[i-1])
{
ans--;
book[i]=0;
}
}
}
cout<<ans<<endl;
for(int i=0;i<n;i++)
{
if(i)
cout<<" ";
if(book[i])
cout<<a[i];
}
cout<<endl;
return 0;
}
输出格式有问题竟然能AC...

• @ 2017-10-10 18:12:09

有重复元素的时候会多输出空格……
呃……可能数据水吧，本来就是个水题不是

• @ 2019-05-26 23:43:47
``````#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[120],i,k,x,j;
bool f[1100];
int comp(int a,int b)
{
if(a<b) return 1;
else return 0;
}
int main()
{
memset(f,0,sizeof(f));
scanf("%d",&n);
j=0;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(f[x]==1) continue;
if(f[x]==0) { j++; a[j]=x; f[x]=1; k++; }
}
sort(a+1,a+k+1,comp);
cout<<k<<endl;
for(i=1;i<=k;i++) cout<<a[i]<<' ';
return 0;
}
``````
• @ 2019-01-04 22:38:23

b=eval(input())
ass=list(map(int,input().split()))
asss=list(set(ass))
asss.sort()
print(len(asss))
for a in asss:
print(a,end=' ')

• @ 2018-09-18 15:08:40

#include<bits/stdc++.h>
using namespace std;
bool a[1008]={false};
int main()
{
int n,b[1008],ans=0,c[1008]={0},temp=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
a[b[i]]={true};
}
for(int i=1;i<=1000;i++)
{
if(a[i])
ans++;
}
sort(b,b+n+1);
printf("%d\n",ans);
printf("%d",b[1]," ");
temp=b[1];
for(int i=2;i<=n;i++)
{
if(b[i]!=temp)
{
cout<<" "<<b[i];
temp=b[i];
}
else
continue;
}
return 0;
}

• @ 2018-09-09 17:11:20

#include<bits/stdc++.h>
using namespace std;
int a[1002];

int main()
{
memset(a,0,sizeof(a));
int m=0,n;
cin>>n;
int b[n];
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++)
a[b[i]]=1;
for(int i=0;i<1001;i++)
if(a[i]==1)
m++;
cout<<m<<endl;
for(int i=0;i<1001;i++)
if(a[i]==1)
cout<<i<<" ";
return 0;
}

• @ 2018-08-18 14:28:44

直接sort+判重即可，水过

``````#include<iostream>
#include<algorithm>
using namespace std;
int a[101];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
ans++;
}
cout<<ans<<endl;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[i-1])
cout<<a[i]<<' ';
}
return 0;
}

``````
• @ 2018-08-08 14:03:56

Pascal党们，我又来了
var
a:array[1..1000] of longint;
n,i,x,t:longint;
begin
fillchar(a,sizeof(a),0);
t:=0;
for i:=1 to n do
begin
inc(a[x]);
end;;
for i:=1 to 1000 do
if a[i]<>0 then inc(t);
writeln(t);
for i:=1 to 1000 do
if a[i]<>0 then write(i,' ');
end.
记住，我就是国服亚索

• @ 2018-06-28 21:27:20

桶排序即可，超简单

``````#include<iostream>
using namespace std;
int main()
{
int n,tmp,a[1001]={0},tot=0;
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>tmp;
a[tmp]=1;
}
for (int i=1;i<=1000;i++)
if (a[i]==1)
tot++;
cout<<tot<<endl;
for (int i=1;i<=1000;i++)
if (a[i]==1)
cout<<i<<' ';
return 0;
}
``````
• @ 2018-03-08 14:00:38

#include<iostream>
#include<algorithm>
using namespace std;
int a[4223];
int main()
{
int num=0,n;
cin>>n;
for (int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for (int i=0;i<n;i++)
{
if (a[i]!=a[i+1]) num+=1;;
}
cout<<num<<endl;
for (int i=0;i<n;i++)
{
if (a[i]!=a[i+1]) cout<<a[i]<<" ";
}

return 0;
}

• @ 2018-03-07 21:55:50
``````#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int temp;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (a[j] > a[j + 1])
{
temp=a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
int cnt = n;
for (int i = 0; i < n-1; i++)
{
if (a[i] == a[i + 1])
cnt--;
}
cout << cnt << endl;
for (int i = 0; i < n - 1; i++)
{
if (a[i] != a[i + 1])
cout << a[i] << " ";
}
cout << a[n - 1];
delete[]a;
return 0;
}
``````
• @ 2018-02-18 16:16:52

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
set<int> arr;
int main()
{
int n;
cin >> n;
int x;
for (int i = 0; i < n; i++)
{
cin >> x;
arr.insert(x);
}
cout << arr.size() << endl;
for (auto x = arr.begin(); x != arr.end(); x++)
cout << *x << " ";
cout << endl;
return 0;
}

• @ 2018-02-03 10:57:46

#include <bits/stdc++.h>
using namespace std;
char b[1010];
int main(){
int n,i,x,s=0;
cin>>n;
for(i=1;i<=n;i++){
cin>>x;
b[x]++;
}
for(i=1;i<=1000;i++)
if(b[i]!=0)
s++;
cout<<s<<endl;
for(i=1;i<=1000;i++)
if(b[i]!=0)
cout<<i<<" ";
return 0;
}

• @ 2017-11-23 13:51:55

#include<bits/stdc++.h>
using namespace std;
int main()
{
int s,n;
cin>>n;
int a[n];
for(s=0;s<n;s++)
cin>>a[s];
sort(a,a+n);
int m=unique(a,a+n)-a;
cout<<m<<endl;
for(s=0;s<m;s++)
cout<<a[s]<<" ";
}

• @ 2017-11-23 13:49:45
``````int n,arr[101]={0},t[101]={0};
int x=0;
char c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c>='0' && c<='9') x=x*10+c-'0', c=getchar();
return x;
}
void merge_sort(int *a, int l, int r , int *t) {
if(r-l>1) {
int m=(l+r)>>1;
int p=l, q=m, i= l;
merge_sort(a,l,m,t);
merge_sort(a,m,r,t);
while(p<m || q<r){
if(q>=r || (p<m && a[p]<a[q])) t[i++] = a[p++];
else t[i++] = a[q++];
}
for(i=l; i<r; i++)  a[i]= t[i];
}
}
int main() {
int c=1,tx;
for(int i=1; i<=n; i++){
}
merge_sort(arr,1,n+1,t);
for(int i=1;i<n;i++){
if(arr[i]==arr[i+1]) continue;
c++;
}
printf("%d\n",c);
for(int i=1;i<n;i++){
if(arr[i]==arr[i+1]) continue;
printf("%d ",arr[i]);
}
printf("%d", arr[n]);
return 0;
}
``````
• @ 2017-10-04 13:01:37

轻松桶排序。
#include <iostream>
using namespace std;

int n,temp;
int arr[1005];

int main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>temp;
arr[temp]++;
}
int number=0;
for(int i=1;i<=1000;i++){
if(arr[i]!=0){
number++;
}
}
cout<<number<<endl;
for(int i=1;i<=1000;i++){
if(arr[i]!=0){
cout<<i<<' ';
}
}
return 0;
}

• @ 2017-10-03 10:46:19

#include<iostream>
#include<set>
#include<algorithm>
#define MAX_N 105
using namespace std;
int N;
int num[MAX_N];
set<int>used;
cin>>N;
for (int i=1;i<=N;i++){
cin>>num[i];
used.insert(num[i]);
}
}
void solve();
int main(){
solve();
return 0;
}
void solve(){
set<int>::iterator i;
cout<<used.size()<<endl;
for (i=used.begin();i!=used.end();i++){
cout<<*i<<" ";
}
}

• @ 2019-02-27 22:12:54

#include<iostream>
#include<list>
using namespace std ;
int main()
{
list<int>v;
list<int>::iterator start,end;
int n,t;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t;
v.push_back(t);
}
v.sort();
v.unique();
cout <<v.size() <<endl ;
for(start = v.begin(),end =v.end() ;start!=end;start++)
{
cout << *start <<" ";
}
return 0;
}

• @ 2019-02-18 12:24:48

/**
*使用快排进行排序然后替换所有重复的值，统计重复数量，进行输出。
*/
import java.util.Scanner;

public class Main {

static int[] a;

public static void f(int left, int right) {
int i, j, temp, t;
i = left;
j = right;
if (left > right) {
return;
}
temp = a[left];
while (i != j) {
while (temp <= a[j] && i < j)
j--;
while (temp >= a[i] && i < j)
i++;
if (i < j) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
a[left] = a[i];
a[i] = temp;
f(left, i - 1);
f(i + 1, right);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
int x = 0;
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
f(0, n - 1);
for (int i = 1; i < a.length; i++) {
if (a[i - 1] == a[i]) {
x++;
a[i - 1] = Integer.MIN_VALUE;
}
}
System.out.println(n - x);
for (int i = 0; i < a.length; i++) {
if (a[i] != Integer.MIN_VALUE) {
if (i < a.length-1)
System.out.print(a[i] + " ");
else
System.out.println(a[i]);
}
}
}
in.close();
}
}

• @ 2019-01-17 17:25:30

这里用桶排相对来说更好
此处代码已优化桶排（仅限于需要去重的时候）

``````#include <iostream>
using namespace std;
bool t[100001] = {false};
int main()
{
int i, j, tot = 0, n;
cin >> j;
for(i = 0; i < j; i++)
{
cin >> n;
if(t[n] == false)
{
t[n] = true;
tot++;
}
}
cout << tot << endl;
for(i = 0; i < 100001; i++)
{
if(t[i])
{
cout << i << " ";
}
}
return 0;
}
``````

ID
1316

3

9362

4526

48%