2018届美团笔试 k的倍数 改考卷。搜狗圆周上点的距离。

题一 k的倍数

这里写图片描述

思路

k的倍数且求的是最长连续子序列。
更改523. Continuous Subarray Sum思路2即可
每次计算得到的余数,若不在hash表中,就插入到hash表中,若余数已经在hash表中,当前位置的索引减去之前的位置,即可得到这个子区间的长度。

 class Solution {  
    public:  
        int checkSubarraySum(vector<int>& nums, int k) {  
            unordered_map<int, int> hash;  
            int sum = 0;  
            hash[0] = -1; 
            int res = 0; 
            for(int i=0; i<nums.size(); ++i) {  
                sum += nums[i];  
                if(k) sum %= k;  
                if(hash.find(sum) != hash.end()) {  
                    res = max((i-hash[sum],res));  
                }  
                else hash[sum] = i;  
            }  
            return res ;  
        }  
    };  

题二 改考卷

这里写图片描述

对于这个题,我们先分析一下,

1.首先,老师选择的第一个组必须是数量最大的组;

假如我们选的第一组不是数量最大的组,对于接下来的每一个组,我们的操作都是先把讲台上的试卷减少s(i)份,然后再增加s(i)份,讲台上的试卷数量稳定是第一组的数量,当我们碰到数量比第一组大的组时,会出现缺少试卷的缺陷,所以老师选择的第一个组必须是数量最大的组;

2.第一组的数量必须小于等于剩下各组的和

如果第一组的数量大于剩下各组的和,那么把第一组的试卷分给剩下的每个组中的同学后,还有剩余,剩余的试卷再分给第一组,就会出现自己改自己试卷的情况,出现缺陷。

3.只要第一组是数量最大的组,那么剩下各组分试卷的顺序是无所谓的

因为我们选择的第一组是数量最大的组,所以讲台上试卷数量始终都会大于等于各组的数量,必然不会出现缺少试卷的情况

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;

int main()
{
    vector<int> array;
    int n;
    cin >> n;
    array.resize(n);
    for (auto i = 0; i < n;++i)
    {
        cin >> array[i];
    }
    int max_ = *max_element(array.begin(), array.end());
    long long sum = accumulate(array.begin(), array.end(), 0);

    if (sum > max_ * 2)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return 0;
}

搜狐 圆周上两点间的距离

这里写图片描述

思路

定义left,right指针索引,同时从数组0开始遍历,
当dis = nums[right]+numbers[left] <= 180,对比更新最大差值maxval,同时right++。
当dis = nums[right]+numbers[left] > 180,差值为360-dis,对比更新maxval,同时left++。
最终使得right停留在最后一个元素位置。
此时只需left++,计算差值dis,更新maxval


#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <limits>
using namespace std;
int mainsogou()
{
    int count;
    vector<double> numbers;
    cin >> count;
    int temp = 0;
    numbers.resize(count);
    for (auto i = 0; i < count; ++i)
    {
        scanf("%lf", &numbers[i]);
    }
    double maxvalue(0);
    int l(0), r(0);
    double cur(0);
    while (1)
    {
        if (r >= numbers.size())
            break;

        cur = numbers[r] - numbers[l];
        if (cur > 180)
        {
            maxvalue = max(maxvalue, 360 - cur);
        }
        else
        {
            maxvalue = max(maxvalue, cur);
            if (maxvalue == 180.00000000)
            {
                return maxvalue;
            }
        }

        if (cur > 180)
            l++;
        else
            r++;
    }
    r--;
    while (1)
    {
        if (l >= numbers.size())
            break;

        cur = numbers[r] - numbers[l];
        if (cur > 180)
        {
            maxvalue = max(maxvalue, 360 - cur);
        }
        else
        {
            maxvalue = max(maxvalue, cur);
        }
        l++;
    }

    cout.flags(ios::fixed);
    cout.precision(8);
    cout << maxvalue << endl;
    return 0;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值