【玲珑杯R7 C】Duplicate Numbers

Time Limit:2s Memory Limit:1024MByte

Submissions:1074Solved:371

DESCRIPTION
Given a list of integers, find all the numbers which appear in the list at least twice.

INPUT
There are
T
T test cases.

For each test case,

The first line contains a number
n
n (0≤
n
n ≤105) — The size of integer list.

The second line contains
n
n numbers (a1, a2, …, an) (|ai| < 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 ’
n
o
n
e
none’

N
o
t
e
.
Note. You should print all duplicate numbers in ascending order.
SAMPLE INPUT
2
5
1 3 3 2 2
5
2 1 3 4 5
SAMPLE OUTPUT
2 3
none

【题目链接】:http://www.ifrog.cc/acm/problem/1073

【题解】

把n个数排个序;
相同的数就会跑到一起;
然后把出现次数大于等于2的加入到ans里面最后输出就好(末尾的空格不好处理吧,所以先存起来);

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int MAXN = 1e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int a[MAXN],n,len;
int ans[MAXN];

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    int T;
    rei(T);
    while (T--)
    {
        len = 0;
        rei(n);
        rep1(i,1,n)
            rei(a[i]);
        sort(a+1,a+1+n);
        rep1(i,1,n)
        {
            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
        {
            rep1(i,1,len)
            {
                printf("%d",ans[i]);
                if (i==len)
                    puts("");
                else
                    putchar(' ');
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值