1073 - Duplicate Numbers
Time Limit:2s Memory Limit:1024MByte
Submissions:1080Solved:373
DESCRIPTION
Given a list of integers, find all the numbers which appear in the list at least twice.
INPUT
There are
TT test cases.For each test case,
The first line contains a number nn (0≤ nn ≤10 5) — The size of integer list.
The second line contains nn numbers (a 1, a 2, …, a n) (|a i| < MAX\_INT ) — The list you need to check duplicated.
The first line contains a number nn (0≤ nn ≤10 5) — The size of integer list.
The second line contains nn numbers (a 1, a 2, …, a n) (|a i| < MAX\_INT ) — The list you need to check duplicated.
OUTPUT
For each test case, return all duplicate numbers in one line (use a space between each two numbers). If you can't found any duplicate number, print '
nonenone'
Note.Note. You should print all duplicate numbers in ascending order.
Note.Note. You should print all duplicate numbers in ascending order.
SAMPLE INPUT
251 3 3 2 252 1 3 4 5
SAMPLE OUTPUT
2 3none
SOLUTION
#include<cstdio>
#include<algorithm>
using namespace std;
#include<cstring>
int main()
{
int T,n,a[100001],ans[100001];
scanf("%d",&T);
while (T--)
{
int len = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
int j = i;
while (j+1<=n && a[j+1]==a[i])
j++;
if (j-i+1>=2)
ans[++len] = a[i];
i = j;
}
if (len == 0)
puts("none");
else
{
for(int i=1;i<=len;i++)
{
printf("%d",ans[i]);
if (i==len)
puts("");
else
putchar(' ');
}
}
}
return 0;
}
我的一直出现格式错误,。。。。
#include<cstdio>
#include<algorithm>
using namespace std;
#include<cstring>
int main()
{
int t,n,a[100001],b[100001],num[100001];
scanf("%d",&t);
while(t--){
memset(num,0,sizeof(num));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
num[a[i]]++;
}
sort(a+1,a+n+1);
int faut=0;
int k=0;
a[0]=1e7;
for(int i=1;i<=n;i++){
if(num[a[i]]>=2&&a[i]!=a[i-1]) {
faut=1;b[k++]=a[i];
}
}
if(faut==0)
printf("none\n");
else
{
for(int i=0;i<k-1;i++)
printf("%d ",b[i]);
printf("%d\n",b[k-1]);
}
}
return 0;
}