头条笔试五道编程题

发下头条的笔试题目,给大家笔试后练习。
第一题:

第二题:


第三题:

第四题:

第五题:

#笔试题目##字节跳动#
全部评论
题目过几天应该会放出来给大家练习
点赞 回复 分享
发布于 2018-08-25 12:16
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() {          int a,b;     scanf("%d%d", &a, &b);     vector<int> ans(a, 0),tmp(a,1);     for (int i = 0;i<a;i++)     {         scanf("%d", &ans[i]);     }     for (int i = 1;i<a;i++)     {         for (int j = 0;j<i;j++)         {             if (ans[i]>=ans[j])             {                 tmp[i] = max(tmp[i], tmp[j] + 1);             }         }     }     cout << *max_element(tmp.begin(), tmp.end())+b-1; } //第四题答案,刚才转念一想,做复杂了。因为是重复的序列,我本来是在全局序列之中求最长上升子序列 其实只需在第一个序列之中求最长上升序列,并且其中的最大元素在后面的每个序列之中一定存在,顾加上 b-1。
点赞 回复 分享
发布于 2018-08-25 19:31
#include <iostream> using namespace std; const int mod = 1e9 + 7; int dp_1[10000],dp_2[10000]; int main() {     int n;     cin >> n;     dp_1[0] = 10;     for (int i = 1;i<n;i++)     {         dp_1[i] = dp_1[i - 1] * 10;     }     dp_2[0] = dp_1[0];     dp_2[1] = dp_1[1];     for (int i = 2;i<n;i++)     {         dp_2[i] = (dp_1[i] + dp_2[i - 2])%mod;         for (int j = 1;j<n-1;i++)         {             dp_2[i] += (dp_2[j] * dp_2[i - j - 1])%mod;         }         dp_2[i] %= mod;     }     cout << dp_2[n - 1]; } 第二题答案。 不能检测了,不敢说对。但思想没毛病。dp_1表示只有字符的情况。dp_2[i]要加上dp_2[i-2], 是考虑括号。最后的循环是考虑加号。
点赞 回复 分享
发布于 2018-08-25 14:40
https://www.jianshu.com/p/0b0f11b89982 第二题还是没什么思路
点赞 回复 分享
发布于 2018-08-28 14:26
这些题感觉非竞赛选手会做自闭的 ……
点赞 回复 分享
发布于 2018-08-27 00:11
//第一题 dfs import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class test1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); Queue<Integer>[] queue = (LinkedList<Integer>[]) new LinkedList[num+1]; for (int i = 1; i <= num; i++){ queue[i] = new LinkedList<>(); } for (int i = 1; i <= num; i++){ int k = scan.nextInt(); while (k != 0){ queue[i].add(k); queue[k].add(i); k = scan.nextInt(); } } boolean[] marked = new boolean[num+1]; //维护一个标记数组 int count = 0; for (int i = 1; i <= num; i++){ if (!marked[i]) { dfs(queue, marked, i); count++; } } System.out.println(count); } public static void dfs(Queue<Integer>[] queue, boolean[] marked, int v){ marked[v] = true; for (int w : queue[v]){ if (!marked[w]) { dfs(queue, marked, w); } } } }
点赞 回复 分享
发布于 2018-08-26 17:03
//第一题并查集思路,供参考 #include <stdio.h> #define N 100020 int friends[N];//每个人所属的连通分量,即构成朋友树时每个人的父节点 int rank[N];//连通分量的权值,即朋友树的大小 int res; void init(int n)//初始化initialization { for(int i=0;i<n;i++) { friends[i]=i; rank[i]=0; } } int findRoot(int x)//寻找x所属的朋友树的根节点 { //一直向上遍历寻找根节点 while(x != friends[x]) x = friends[x]; return x; } void connect(int x,int y) { int xRoot = findRoot(x); int yRoot = findRoot(y); if(xRoot == yRoot) return ; //判断树高,小树并在大树下 if(rank[xRoot] < rank[yRoot]) friends[xRoot]=yRoot; else { friends[yRoot] = xRoot; if(rank[xRoot]==rank[yRoot])//两树高相等,合并后树高+1 rank[xRoot]++; } --res; } int main() { int n; init(N);//初始化 scanf("%d",&n); res = n; for(int i=1;i<=n;i++){ int t; while(~scanf("%d",&t)){ if(t == 0) break; connect(i,t); } } printf("%d",res); return 0; }
点赞 回复 分享
发布于 2018-08-26 14:08
//第一题DFS思路,空间还能优化,没提交,不知道对不对 #include <iostream> #include <vector> using namespace std; int n; int res; void dfs(vector<vector<int>>& friends, int x, int y,vector<vector<bool>>& mark){ if(x >= friends.size() || y >= friends[0].size() || x < 0 || y < 0) return; if(mark[x][y] == true) return; if(friends[x][y] == 0){ mark[x][y] = true; return; } // 对于已经搜索过的点要进行标记 mark[x][y] = true; res--; for(int j=1; j<n; j++){ dfs(friends, x, j, mark); } } void minM(vector<vector<int>>& friends) { if(friends.empty()) return; res = n; vector<vector<bool>> vecMark(friends.size(),vector<bool>(friends[0].size(),false));// 定义标记数组 //开始搜索 for(int i = 1;i < friends.size();i++){ for(int j = 1;j < friends[0].size();j++){ if(vecMark[i][j] == true) continue; if(friends[i][j] == 0){ vecMark[i][j] = true; continue; } dfs(friends, i, j, vecMark); } } cout << num << endl; } int main() { cin >> n; vector<vector<int>> friends(n+1, vector<int>(n+1,0)); int temp = 0; for(int i=1; i<=n; i++){ int j = 1; while(cin>>temp){ if(temp == 0) break; friends[i][j] = temp; j++; } } minM(friends); return 0; }
点赞 回复 分享
发布于 2018-08-26 14:07
太难了
点赞 回复 分享
发布于 2018-08-25 12:57
第五题我看输出格式就没有做的动力了。
点赞 回复 分享
发布于 2018-08-25 12:52
我也截图留下来了 两次都有(笑哭)
点赞 回复 分享
发布于 2018-08-25 12:39
坐等答案。
点赞 回复 分享
发布于 2018-08-25 12:25
坐等答案!!
点赞 回复 分享
发布于 2018-08-25 12:20
等答案+1
点赞 回复 分享
发布于 2018-08-25 12:19
第三题读取一直有问题...最后没有回车怎么解决
点赞 回复 分享
发布于 2018-08-25 12:16
等答案
点赞 回复 分享
发布于 2018-08-25 12:16
第一题不是bfs么?各种超时
点赞 回复 分享
发布于 2018-08-25 12:15
等答案
点赞 回复 分享
发布于 2018-08-25 12:14
抢沙发,等解答
点赞 回复 分享
发布于 2018-08-25 12:11

相关推荐

不愿透露姓名的神秘牛友
05-20 16:14
已编辑
不止遇到一次了,什么都不会,让提合并请求,问什么是合并请求。让gitlab.页面把测试截图附上,不知道截图要放在哪,那么大的编辑看不到吗让配开发机,问ip是什么东西……这都咋进来的啊,我们(我2023年毕业)那会儿没AI的时候面试都是直接linux,docker,k8s,git,结构与算法,计网。怎么才过去2年,实习生跟傻子一样,有些问题问的我难受,不会git&nbsp;commit,不会git&nbsp;pull,不会切换分支,直接要覆盖master....————而且态度非常敷衍,3天前给开个仓库权限,连本地都没有拉下来。让写一个小文档,都是说一句,写一句,说把目录加上,挺嗤之以鼻,最后还是把目录加上了😂😂任何文档和注释都是方便后来人的,现在的人真的很自负啊,打开github看看任何一个开源项目的文档和注释,都写的很详细。难道现在的同学在校期间不经常拉开源项目看源码学习吗?&nbsp;哪怕是一个swap函数,开源项目里都经常注释:1&nbsp;3&nbsp;5&nbsp;7&nbsp;9&nbsp;2&nbsp;4&nbsp;6&nbsp;8&nbsp;10^&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rswap:{功能描述}{使用样例}————给我气笑了,没次问我有什么任务的时候,我都是说,优先你学校导师的项目,然后再做公司需求。然后给了两个需求,一个月内搞定就行,既然是agent开发,1.&nbsp;部署需要维护项目的开发环境2.阅读opencode/openclaude代码(我个人感觉龙虾的源码agent部分很常规,就一个channel+agent,还不如看claude泄露的代码和opencode)然后任务1搞了几周说因为环境问题,他申请到的远程开发机是linux,装的python2,项目是py3的,所以没搭建,我说你不行就用conda或docker把环境屏蔽了呢,没搭理我。任务2:看了很长时间代码,给我回了一句,opencode和openclaude是用go写的……我说你打开github看右下角那的语言是ts还是go……&nbsp;结果满脸懵的说ts是什么……我让看agent&nbsp;loop,哪怕全局搜索一下while(true),跳过去从头看到尾就大致清楚了,压根没看。————嘻嘻,我已经开始做社招简历了。
redf1sh:默认会git结果发现真不会,这种一看就是没做过项目的,真做过项目的至少会提交
点赞 评论 收藏
分享
评论
点赞
148
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务