今日头条2018春招研发岗第一次笔试题解

第一题:
双指针:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+7;
int a[N];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i) scanf("%d",&a[i]);
    sort(a, a+n);
    n = unique(a, a+n) -a;
    int r = 0, ans=0;
    for(int l=0; l<n;++l)
    {
        while(r<n&&a[r]-a[l]<k) ++r;
        if(r==n) break;
        if(a[r]-a[l] == k) ++ans;
    }
    printf("%d\n", ans);
    return 0;
}
第二题:
BFS
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
map<pair<int,int> , int > mp;
int main()
{
    int n;
    scanf("%d",&n);
    queue<pii> q;
    q.push(make_pair(1, 1));
    mp[make_pair(1,1)]=0;
    while(!q.empty())
    {
        pii pr = q.front();q.pop();
//        printf("%d %d\n",pr.first, pr.second);
        if(pr.first == n)
        {
            printf("%d\n", mp[pr]);
            exit(0);
        }
        pii t=pr;
        t.second = t.first; t.first*=2;
        if(!mp.count(t))
        {
            q.push(t);
            mp[t] = mp[pr]+1;
        }
        t=pr;
        t.first=t.first+t.second;
        if(!mp.count(t))
        {
            q.push(t);
            mp[t] = mp[pr]+1;
        }
    }
    return 0;
}
第三题:
模拟。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[107];
char G[5][10][8] = {
    {"66666", "....6", "66666", "66666", "6...6", "66666", "66666", "66666", "66666", "66666"},
    {"6...6", "....6", "....6", "....6", "6...6", "6....", "6....", "....6", "6...6", "6...6"},
    {"6...6", "....6", "66666", "66666", "66666", "66666", "66666", "....6", "66666", "66666"},
    {"6...6", "....6", "6....", "....6", "....6", "....6", "6...6", "....6", "6...6", "....6"},
    {"66666", "....6", "66666", "66666", "....6", "66666", "66666", "....6", "66666", "66666"}
};
ll cal()
{
    int n = strlen(s);
    ll sum=0, cur=0, prd=1;
    for(int i=0; i<n; ++i)
    {
        if(isdigit(s[i])) cur=cur*10+s[i]-'0';
        else if(s[i] == '-')
        {
            sum+=prd*cur;
            cur=0;
            prd=-1;
        }
        else if(s[i] == '+')
        {
            sum+=prd*cur;
            cur=0;
            prd=1;
        }
        else
        {
            prd*=cur;
            cur=0;
        }
    }
    return sum+prd*cur;
}
int main()
{
    int T;
    scanf("%d",&T);while(T--)
    {
        scanf("%s", s);
        ll ans = cal();
        for(int i=0; i<5; ++i)
        {
            vector<int> v;
            ll tmp = ans;
            while(tmp) v.push_back(tmp%10),tmp/=10;
            reverse(v.begin(), v.end());
            if(v.empty()) v.push_back(0);
            for(int j=0; j<v.size(); ++j)
            {
                printf("%s%s",G[i][v[j]], j+1==v.size()?"\n":"..");
            }
        }
    }
    return 0;
}


第四题:
只能从均值大的集合往均值小的集合里放。
取数只能取出现次数等于1的数
放数只能放没出现过的数
从小的数开始放可以使均值小的集合均值上升慢,均值大的集合均值上升快,这样最优。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int> sa,sb;
ll suma, sumb;
const long double eps = 1e-14;
inline int cmp(long double a, long double b)
{
    if(fabs(a-b) <= eps) return 0;
    return a>b?1:-1;
}
inline long double jz(ll k, int m)
{
    return (long double)k/m;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n;++i)
    {
        int t;
        scanf("%d",&t);
        sa.insert(t);
        suma+=t;
    }
    for(int i=0;i<m;++i)
    {
        int t;
        scanf("%d",&t);
        sb.insert(t);
        sumb+=t;
    }
    if(cmp(jz(suma, n), jz(sumb, m))==-1)
    {
        swap(suma, sumb);
        swap(n, m);
        sa.swap(sb);
    }
    int mx =n;
    int ans = 0;
    for(auto k : sa)
    {
        if(cmp(k, jz(suma, n)) >= 0) break;
//        printf("%d %d\n",n, k);
        if(!sb.count(k)&&cmp(k, jz(sumb, m))>0)
        {
            ++ans;
            sumb+=k;
            suma-=k;
            sb.insert(k);
            --n;++m;
        }
    }
    printf("%d\n", ans);


第五题:BFS
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+1000;
typedef pair<int, int> pii;
bool vis[N];
int a[N];
int main()
{
    int n,k,h;
    scanf("%d%d%d",&n,&k,&h);
    for(int i=0;i<n;++i)
    {
        int t;
        scanf("%d",&t);
        a[t]=1;
    }
    queue<pii> q;
    q.push({0,0});
    int ans = 0;
    while(!q.empty())
    {
        pii p = q.front(); q.pop();
        if(p.second>k) break;
        ans = max(ans, p.first);
        for(int i=1; i<=h; ++i)
        {
            if(a[p.first + i]&&!vis[p.first+2*i])
            {
                vis[p.first+2*i]=true;
                q.push(make_pair(p.first+2*i, p.second+1));
            }
            if(p.first-2*i>0&&a[p.first-i]&&!vis[p.first-2*i])
            {
                vis[p.first-2*i]=true;
                q.push(make_pair(p.first-2*i, p.second+1));
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

全部评论
给大佬递烟。。
点赞 回复
分享
发布于 2018-03-24 21:16
老哥,吃橘子么,我去把橘子树砍下来
点赞 回复
分享
发布于 2018-03-24 21:16
联想
校招火热招聘中
官网直投
大佬缺女票吗
点赞 回复
分享
发布于 2018-03-24 21:18
#include<iostream> using namespace std; int main() { while(1) { cout<<"大佬酷酷酷酷酷酷酷酷"; } return 0; }
点赞 回复
分享
发布于 2018-03-24 21:23
第一题: n,k=(int(i) for i in raw_input().strip().split(' ')) l=[int(i) for i in raw_input().strip().split(' ')] ls=[j+k for j in l] print len(set(l)&set(ls))
点赞 回复
分享
发布于 2018-03-24 21:38
大佬投的什么岗,要是不投算法岗就可惜了
点赞 回复
分享
发布于 2018-03-24 22:56
兄弟 牛皮
点赞 回复
分享
发布于 2018-03-24 21:11
。。。。大佬
点赞 回复
分享
发布于 2018-03-24 21:12
给大佬递茶
点赞 回复
分享
发布于 2018-03-24 21:12
大佬
点赞 回复
分享
发布于 2018-03-24 21:13
qianpai...
点赞 回复
分享
发布于 2018-03-24 21:13
递茶递茶
点赞 回复
分享
发布于 2018-03-24 21:13
点赞 回复
分享
发布于 2018-03-24 21:14
Orz 递茶递茶
点赞 回复
分享
发布于 2018-03-24 21:14
牛逼
点赞 回复
分享
发布于 2018-03-24 21:14
大佬
点赞 回复
分享
发布于 2018-03-24 21:15
好厉害哇
点赞 回复
分享
发布于 2018-03-24 21:15
兄弟牛逼!
点赞 回复
分享
发布于 2018-03-24 21:15
牛皮666666666666666666
点赞 回复
分享
发布于 2018-03-24 21:15
不服不行
点赞 回复
分享
发布于 2018-03-24 21:16

相关推荐

小公司 项目经理助理 实习3k转正6k+
点赞 评论 收藏
转发
头像
不愿透露姓名的神秘牛友
03-11 09:15
小米 产品经理 18x15 硕士海归
点赞 评论 收藏
转发
头像
不愿透露姓名的神秘牛友
03-04 09:20
杭州移动 客户经理 23-25W 硕士海归
点赞 评论 收藏
转发
点赞 184 评论
分享
牛客网
牛客企业服务