今日头条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 22:56
第一题: 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
#include<iostream> using namespace std; int main() { while(1) { cout<<"大佬酷酷酷酷酷酷酷酷"; } return 0; }
点赞 回复 分享
发布于 2018-03-24 21:23
大佬缺女票吗
点赞 回复 分享
发布于 2018-03-24 21:18
老哥,吃橘子么,我去把橘子树砍下来
点赞 回复 分享
发布于 2018-03-24 21:16
莫非是卓爷
点赞 回复 分享
发布于 2018-04-11 20:43
为什么我投的前端岗 笔试的都是这种算法题
点赞 回复 分享
发布于 2018-03-28 21:20
请问大佬们。这题还有能交的地方吗
点赞 回复 分享
发布于 2018-03-27 18:56
大。。。大大佬!!
点赞 回复 分享
发布于 2018-03-27 14:29
package codetest1; import java.util.*; public class Test2 {     static class Node {         int m;         int s;         int count;                  public Node(){             this.m=0;             this.s=0;             this.count=0;         }     }          public static int findMin(Node start,int n){                  Queue<Node> queue = new LinkedList<Node>();         queue.add(start);         while(!queue.isEmpty()){             Node node = queue.poll();             if(node.s == n){                 return node.count;             }else if(node.s<n){                 Node node1 = new Node();                 Node node2 = new Node();                 int count = ++node.count;                 node1.m = node.s; node1.s = node.s*2; node1.count=count;                 node2.m = node.m; node2.s = node.s+node.m; node2.count=count;                 queue.add(node1);                 queue.add(node2);             }         }         return -1;              }     public static void main(String[] args){         Node start = new Node();         start.s=1;start.m=1;start.count=0;         long time = System.currentTimeMillis();         int result = findMin(start, 9999);         System.out.println("\r<br>执行耗时 : "+(System.currentTimeMillis()-time)/1000f+" 秒 ");         System.out.println(result);     }           } #include<iostream> #include<queue> #include<time.h> using namespace std; class Node { public:         int m;         int s;         int count;         Node(){             this->m = 0;             this->s = 0;             this->count = 0;         }         Node(int m, int s, int count){             this->m = m;             this->s = s;             this->count = count;         } }; int findMin(Node* start, int n){     queue<Node*> q;     q.push(start);     while (!q.empty()){             Node* node = q.front();             q.pop();             if (node->s == n){                 return node->count;             }             else if (node->s<n){                 int count = ++node->count;                 q.push(new Node(node->s,node->s*2,count));                 q.push(new Node(node->m,node->s+node->m,count));             }             delete node;         }         return -1;     }     int main(){         clock_t starttime, finishtime;         Node* start = new Node(1,1,0);         starttime = clock();         int result = findMin(start,9999);         finishtime = clock();         cout <<"时间:"<<(double)(finishtime-starttime)/CLOCKS_PER_SEC<<" "<< result << endl;         //System.out.println("\r<br>执行耗时 : " + (System.currentTimeMillis() - time) / 1000f + " 秒 ");         system("pause");         //.out.println(result);     } 同样的思想用的广搜,为什么c++的代码比java的慢这么多呀
点赞 回复 分享
发布于 2018-03-26 21:12
西电的大佬啊 。。。。。是大三吗?
点赞 回复 分享
发布于 2018-03-25 19:51
膜拜大佬,最后一题什么意思啊?没看懂
点赞 回复 分享
发布于 2018-03-25 15:50
大佬,缺大腿挂件吗
点赞 回复 分享
发布于 2018-03-25 14:43
第一题不是返回去重之后的数组吗?大佬的代码是返回的字数对吗???
点赞 回复 分享
发布于 2018-03-25 14:15
#include <iostream> using namespace std; int const N=100001; int numbers[N]; int main() {     int n,k,temp,count=0,max=0;     cin>>n;cin>>k;     for (int i = 0; i < n;i++)     {         cin>>temp;         numbers[temp]++;                  if (max < temp) {             max = temp;         }     }        for (int i = 0; i<=max; i++)     {        if (k == 0)        {            if (numbers[i])                count++;        }else{            if (numbers[i] && numbers[i+k] )                count++;        }     }     cout<<count<<endl;     return 0; 斗求大佬评析, }
点赞 回复 分享
发布于 2018-03-25 11:22
真的大神 学习了,感谢
点赞 回复 分享
发布于 2018-03-25 11:09
有python写的代码吗?=。=
点赞 回复 分享
发布于 2018-03-25 10:10
牛比
点赞 回复 分享
发布于 2018-03-25 10:01
-
点赞 回复 分享
发布于 2018-03-25 09:35

相关推荐

时雨h:人生就像站在岔路口,两个方向都可以先了解了解,就像罗伯特·弗罗斯特诗里说的,“黄色的树林里分出两条路,可惜我不能同时去涉足” ,这两个方向就如同那两条路,每条都有独特的风景与未知。 除了自行探索,也可以看看自己学校往年同专业学长学姐的去向,每一届大致都差不多,这能帮你找到自己的定位。多跟他们交流交流,听听他们在不同选择中的收获与遗憾,那些过来人的经验会成为你前行路上的微光。 做出选择后,固然要坚定自己的选择,勇往直前地走下去,但也别忘了,那条未选择的路也始终在那里,它或许代表着另一种可能,另一种人生轨迹。偶尔回望,它能让你更加明白自己当下选择的价值,也能让你在前行的路上,多一份思考与从容。
点赞 评论 收藏
分享
评论
点赞
184
分享

创作者周榜

更多
牛客网
牛客企业服务