快手2018春季校园招聘笔试后端A卷编程题

有点简单了。(逃)

第一题:计算(x^y) % N

快速幂就好了,应该都会

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL dfs(LL x, LL y, LL N)
{
    if (y == 0)
        return 1;
    LL ret = dfs(x, y / 2, N);
    if (y % 2 == 0)
        return ret * ret % N;
    return ret * ret * x % N;
}

int main()
{
    LL x, y, N;
    cin >> x >> y >> N;
    cout << dfs(x, y, N) << endl;
    return 0;
}

第二题:二分查找

直接调用lower_bound就好了,注意下输入就好了。

应该都会。

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int x;
    char c;
    vector<int> arr;
    while (true) {
        scanf("%d", &x);
        arr.push_back(x);
        if (getchar() == '\n')
            break;
    }
    scanf("%d", &x);
    printf("%u\n", lower_bound(arr.begin(), arr.end(), x) - arr.begin());
    return 0;
}
#春招#
全部评论
我第一道题忘了写就提交了,心塞
点赞 回复
分享
发布于 2018-04-20 20:29
我第二题竟然纠结读取两行数据格式,没做出来,不知道还有这种神操作,膜拜大佬
点赞 回复
分享
发布于 2018-04-20 20:58
滴滴
校招火热招聘中
官网直投

相关推荐

点赞 4 评论
分享
牛客网
牛客企业服务