腾讯2017暑期实习生编程题-C-有趣的数字

ACM模版

描述

描述

题解

这个招聘实习生的编程题真心简单,可是我在牛客网上做的,莫名其妙的 WA,但是我思路肯定没问题啊,本地测试的样例也是过的,可是系统提示说我连样例都错了,这就尴尬了,感觉牛客网的测评系统好垃圾啊~~~

首先排序,然后分别求最大数最小数出现的次数,然后根据乘法原理得差最大对儿数,接着,求相邻两个差最小的值,如果为0,说明存在重复数字,求重复最多的次数,然后根据等差数列求差最小对儿数,否则,遍历一遍,求最小差值出现过多少次。

代码

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int MAXN = 1e5 + 10;

int a[MAXN];

int main(int argc, const char * argv[])
{
    int n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", a + i);
        }
        sort(a, a + n);

        int tMin = a[0], numMin = 0;
        int tMax = a[n - 1], numMax = 0;
        if (tMin == tMax)
        {
            int ans = n * (n - 1) / 2;
            cout << ans << ' ' << ans << '\n';
            continue;
        }

        for (int i = 0; i < n; i++)
        {
            if (a[i] == tMin)
            {
                numMin++;
            }
            else
            {
                break;
            }
        }
        for (int i = n - 1; i >= 0; i--)
        {
            if (a[i] == tMax)
            {
                numMax++;
            }
            else
            {
                break;
            }
        }
        int resMax = numMin * numMax;

        tMin = a[1] - a[0];
        for (int i = 0; i < n - 1; i++)
        {
            if (a[i + 1] - a[i] < tMin)
            {
                tMin = a[i + 1] - a[i];
            }
        }

        int resMin = 0;
        if (tMin != 0)
        {
            for (int i = 0; i < n - 1; i++)
            {
                if (a[i + 1] - a[i] == tMin)
                {
                    resMin++;
                }
            }
        }
        else
        {
            for (int i = 0; i < n - 1; i++)
            {
                if (a[i] == a[i + 1])
                {
                    int temp = 2;
                    for (int j = i + 2; j < n; j++)
                    {
                        if (a[j] == a[i])
                        {
                            temp++;
                        }
                        else
                        {
                            i = j;
                            break;
                        }
                    }
                    resMin += temp * (temp - 1) / 2;
                }
            }
        }

        cout << resMin << ' ' << resMax << '\n';
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值