371 条题解
-
018017894 (18017894) LV 8 @ 2017-10-31 19:33:04
#include<stdio.h>
int n,x,m;
bool f[1001];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
**scanf("%d",&x);
if (!f[x])
{
m++;
f[x]=true;
} **
}
printf("%d\n",m);
for (int i=1;i<1001;i++)
if (f[i])
printf("%d ",i);
return 0;
} -
02017-10-08 16:58:22@
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=105;int n;
int a[maxn],f[maxn*10];main()
{
memset(f,0,sizeof(f));
int x,k=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
if(f[x]==0)
{
f[x]=1;
a[++k]=x;
}
}
sort(a+1,a+k+1);
cout<<k<<endl;
for(int i=1;i<=k;i++)
cout<<a[i]<<' ';
cout<<endl;
return 0;
} -
02017-09-24 08:53:23@
c++,low爆了
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int x[1010],a,n;
int main()
{
//freopen("random.in","r",stdin);
//freopen("random.out","w",stdout);
cin>>n;
for(int i=0;i<n;i++) cin>>x[i];
sort(x,x+n);
for(int i=0;i<1000;i++) if(x[i]==x[i+1]) x[i]=0;
for(int i=0;i<1000;i++) if(x[i]) a++;
cout<<a<<endl;
for(int i=0;i<1000;i++) if(x[i]) cout<<x[i]<<" ";
return 0;
} -
02017-05-29 16:57:55@
#include<iostream> #include<set> using namespace std; int main(void) { set<int>a; int n=0; cin>>n; for(int i=0;i<n;i++) { int num=0; cin>>num; a.insert(num); } set<int>::iterator iter; cout<<a.size()<<endl; for(iter=a.begin();iter!=a.end();iter++) { cout<<*iter<<' '; } }
第一遍眼瞎没看到要输出个数。。。前几天用了一下map,今天看到这道题就想到了set(红黑树就是强,虽然我都不知道啥东西,
-
02017-04-17 13:31:28@
stl!!!
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> v; while (n--) { int num; cin >> num; if (count(v.begin(), v.end(), num) == 0) // 在前面的输入中不存在 num v.push_back(num); // 在 v 中加入 num } sort(v.begin(), v.end()); // 排序 cout << v.size() << endl; // v 的长度 for (size_t i = 0; i < v.size(); i++) // 注意是 size_t (c++11) cout << v[i] << " "; // 遍历输出 cout << endl; return 0; }
-
02016-11-18 10:41:51@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[10000];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
int count=n;
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(a[i]==a[j] && a[j]!='*'){
a[j]='*';
count--;
}
}
}
cout<<count<<endl;
for(int i=1;i<=n;i++){
if(a[i]!='*'){
cout<<a[i]<<" ";
}
}
cout<<endl;
return 0;
} -
02016-09-11 20:24:07@
#include<iostream> #include<set> #include<iterator> using namespace std; set<int> s; int main() { int m,n; cin>>m; for(int i=1;i<=m;i++) { cin>>n; s.insert(n); } cout<<s.size()<<endl; for(set<int>::iterator it=s.begin();it!=s.end();it++) cout<<*it<<' '; return 0; }
-
02016-08-21 15:51:31@
测试数据 #0: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 508 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 512 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 516 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 516 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 512 KiB, score = 10
Accepted, time = 0 ms, mem = 516 KiB, score = 100
代码
#include<cstdio>const int N= 1003;
bool a[N];int read(){
static char ch;
static int x;
for(ch= getchar(); ch<'0' || ch>'9'; ch= getchar());
for(x= ch-'0', ch= getchar(); ch>='0' && ch<='9'; x= x*10+ch-'0', ch= getchar());
return x;
}int main(){
register int n;
n= read();
register int i;
for(i= 0; i<n; i++) a[read()]= 1;
n= 0;
for(i= 1; i<1001; i++) n+= a[i];
printf("%d\n", n);
for(i= 1; i<1001; i++) if(a[i]) printf("%d ", i);
return 0;
}
理论上没法再快了 -
02016-07-22 20:37:41@
这题简直就是为unique量身定做的。。。
```c++
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;const int maxn = 100 + 5;
int n;
int num[maxn];int main ()
{
cin >> n;
for (int i = 0; i < n; i++) cin >> num[i];
sort(num, num+n);
int m = unique(num, num+n) - num;
cout << m << "\n";
for (int i = 0; i < m; i++) cout << num[i] << " ";
return 0;
}
``` -
02016-07-12 15:56:55@
foo.cpp:5:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
测试数据 #0: Accepted, time = 15 ms, mem = 560 KiB, score = 10
测试数据 #1: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 556 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 560 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 556 KiB, score = 10
Accepted, time = 45 ms, mem = 560 KiB, score = 100
为何不能秒杀?! -
02016-05-26 19:45:52@
var i,j,k,n,l,t:longint;
a:array[1..10000] of longint;
begin
read(n);
k:=n;
for i:=1 to k do
begin
read(l);
a[l]:=a[l]+1;
if a[l]>1 then
begin
a[l]:=1;
n:=n-1;
end;
end;
writeln(n);
for i:=1 to 1000 do
begin
if a[i]=1 then inc(t);
if t=n then
begin
writeln(i);
break;
end;
if a[i]=1 then write(i,' ');
end;
end. -
02016-05-23 12:57:56@
#include<iostream>
using namespace std;
int n,t,count,a[1001];
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t;
a[t]++;
}
for(int i=0;i<1000;i++)
{
if(a[i]>0)
count++;
}
cout<<count<<endl;
for(int i=0;i<1000;i++)
{
if(a[i]>0)cout<<i<<" ";
}
return 0;
} -
02016-05-20 18:01:27@
排序经典老题
var
n,i,num:longint;
s,a:array[1..100000] of longint;
procedure qsort(l,r:longint);
var
i,j,p,mid:longint;
begin
i:=l; j:=r;
mid:=a[(l+r) div 2];
repeat
while a[i]<mid do inc(i);
while a[j]>mid do dec(j);
if i<=j then begin
p:=a[i];
a[i]:=a[j];
a[j]:=p;
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
qsort(1,n);
num:=1;
s[1]:=a[1];
for i:=1 to n do begin
if s[num]<>a[i] then begin
num:=num+1;
s[num]:=a[i];
end;
end;
writeln(num);
for i:=1 to num do begin
write(s[i],' ');
end;
end. -
02016-05-16 14:58:37@
测试数据 #0: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #2: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 804 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 804 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 808 KiB, score = 10
Accepted, time = 45 ms, mem = 808 KiB, score = 100var a:array[1..100] of integer;
b:array[1..1000] of integer;
s,i,n:integer;
begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
for i:=1 to n do b[a[i]]:=1;
s:=0;
for i:=1 to 1000 do if b[i]=1 then inc(s);
writeln(s);
for i:=1 to 1000 do if b[i]=1 then write(i,' ');
writeln;
end. -
02016-05-15 23:32:25@
测试数据 #0: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 488 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 488 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 496 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 492 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 492 KiB, score = 10
include <stdio.h>
int a[1001];
int main (void)
{
int n, i, val, cnt, flag;
scanf("%d",&n);
for (i=0, cnt=0; i<n; i++) {
scanf("%d",&val);
if (a[val] == 0)
cnt++;
a[val]++;
}
printf("%d\n",cnt);
for (i=1,flag=1; i<1001; i++) {
if (flag && a[i]!=0) {
printf("%d",i);
flag = 0;
}
else if (a[i]!=0) {
printf(" %d",i);
}
}
putchar('\n');
return 0;
}
数据量不大用桶排序 -
02016-05-14 17:30:35@
var
a:array[1..101] of integer;
n,i,x:integer;
procedure qsort(head,tail:integer);
var
mid,i,j,temp:integer;
begin
mid:=a[(head+tail)div 2];
i:=head;
j:=tail;
while i<=j do begin
while a[i]<mid do inc(i); while a[j]>mid do dec(j);
if i<=j then begin
temp:=a[i]; a[i]:=a[j]; a[j]:=temp; inc(i); dec(j); end;
end;
if head<j then qsort(head,j); if tail>i then qsort(i,tail);
end;
begin
readln(n);
For i:=1 to n do
read(a[i]);
readln;
qsort(1,n);
x:=n;
For i:=2 to n do
if a[i]=a[i-1] then x:=x-1;
writeln(x);
write(a[1],' ');
For i:=2 to n do
if a[i]<>a[i-1] then write(a[i],' ');
writeln;
readln;
end. -
02016-05-14 17:30:22@
var
a:array[1..101] of integer;
n,i,x:integer;
procedure qsort(head,tail:integer);
var
mid,i,j,temp:integer;
begin
mid:=a[(head+tail)div 2];
i:=head;
j:=tail;
while i<=j do begin
while a[i]<mid do inc(i); while a[j]>mid do dec(j);
if i<=j then begin
temp:=a[i]; a[i]:=a[j]; a[j]:=temp; inc(i); dec(j); end;
end;
if head<j then qsort(head,j); if tail>i then qsort(i,tail);
end;
begin
readln(n);
For i:=1 to n do
read(a[i]);
readln;
qsort(1,n);
x:=n;
For i:=2 to n do
if a[i]=a[i-1] then x:=x-1;
writeln(x);
write(a[1],' ');
For i:=2 to n do
if a[i]<>a[i-1] then write(a[i],' ');
writeln;
readln;
end. -
02016-05-14 17:29:00@
include<iostream>
using namespace std;
int N;
int i;
int x;
int a[1001];
int sum;
int main()
{
cin>>N;
for(i=0;i<N;i++) { cin>>x;
a[x]++;
}
for(i=0;i<=1000;i++)
{
if(a[i]>0)
sum++;
}
cout<<sum<<endl; for(i=0;i<=1000;i++) { if(a[i]>0)
cout<<i<<" ";
}
return 0;
} -
02016-05-14 17:13:08@
var k,n,i:longint;
a,s:array[1..100] of longint;
b:set of 1..100;begin
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n do
b:=b+[a[i]];
k:=1;
for i:=1 to 1000 do
if i in b then
begin
s[k]:=i;
inc(k);
end;
n:=k-1;
for i:=1 to n do
write(s[i],' ');
readln;
readln;
end. -
02016-05-14 17:09:33@
var a,b:array[1..10000]of integer;
t,N,i,j:integer;
procedure suiji(head,tail:integer);
var i,j,empty,mid:integer;
begin
i:=head;j:=tail;mid:=a[(head+tail)div 2];
while (i<=j) do
begin
while a[i]<mid do inc(i);
while a[j]>mid do dec(j);
if i<=j then begin
empty:=a[i]; a[i]:=a[j]; a[j]:=empty;
inc(i); dec(j);
end;
end;
if i<tail then suiji(i,tail);
if j>head then suiji(head,j);
end;begin
t:=0;
readln(N);
for i:=1 to N do
read(a[i]);
for i:=1 to N-1 do
for j:=i+1 to N do
if a[i]=a[j]
then begin a[j]:=0; t:=t+1; end;
writeln(N-t+1);
suiji(1,N);
for i:=1 to N do
if a[i]<>0 then write(a[i],' ');
end.