1073 - Duplicate Numbers

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.
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.
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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值