京东2015年应届生招聘笔试题(A)卷答案编程题和附加题部分
2014-10-12 21:25
375 查看
二.编程题
1.题目大意:编写一个函数func(int n),使得返回值是最小的各位乘积等于n的数,且返回值至少两位数。(题目有点看不太清,我就按这样理解了。如果和题目有出入稍微修改一下应该就能解决。)
C++代码:
2.使用非递归方式实现二叉树的先序遍历,并将节点的值保存在数组中。
C++函数原型:
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
};
void TraverseTreeInPreOrder(std:vector<int>&value,const TreeNode* root) {
}
思路:最基础的搜索按照某一种方式来分可以分为深度优先搜索和广度优先搜索,深度优先搜索使用栈,或者说递归;广度优先搜索使用队列。这里要求非递归,则采用队列。
C++代码(未实现,C++基础不是太好,不过程序能够体现算法细节,仅作参考):
附加题:写出第k个素因子只有2、3、5的数。
因为数的个数相对较少,所以可以初始化。
C++代码:
1.题目大意:编写一个函数func(int n),使得返回值是最小的各位乘积等于n的数,且返回值至少两位数。(题目有点看不太清,我就按这样理解了。如果和题目有出入稍微修改一下应该就能解决。)
C++代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int func(int n) { int ans = 0 , t = 1; for(int i=9;i>1;i--) { while(n % i == 0) { n /= i; ans = ans + t * i; t *= 10; } } if(n != 1) return -1; if(ans == 0) ans = 1; if(ans < 10) ans += 10; return ans; } int main() { int n; while(scanf("%d" , &n) != EOF) { int ans = func(n); cout << ans << endl; } return 0; }
2.使用非递归方式实现二叉树的先序遍历,并将节点的值保存在数组中。
C++函数原型:
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
};
void TraverseTreeInPreOrder(std:vector<int>&value,const TreeNode* root) {
}
思路:最基础的搜索按照某一种方式来分可以分为深度优先搜索和广度优先搜索,深度优先搜索使用栈,或者说递归;广度优先搜索使用队列。这里要求非递归,则采用队列。
C++代码(未实现,C++基础不是太好,不过程序能够体现算法细节,仅作参考):
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vecotr> #include <queue> using namespace std; struct TreeNode { int value; TreeNode* left; TreeNode* right; }; void TraverseTreeInPreOrder(std:vector<int>&value,const TreeNode* root) { TreeNode*u ; queue<TreeNode*> que; que.push(root); while(!que.empty()) { u = que.front(); que.pop(); value.push(u->value); if(u->left != null) que.push(u->left); if(i->right != null) que.push(u->right); } }
附加题:写出第k个素因子只有2、3、5的数。
因为数的个数相对较少,所以可以初始化。
C++代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 10000001;//假设数的范围不会超过1000万 int a[1010] , cnt = 0; bool vis[maxn]; void dfs(int x) { if(x >= maxn || vis[x]) return; vis[x] = true; a[cnt++] = x; dfs(x*2); dfs(x*3); dfs(x*5); } int KthNumber(int k) { return a[k-1]; } int main() { dfs(1); sort(a , a+cnt); int k; while(scanf("%d" , &k) != EOF) { int ans = KthNumber(k); cout << ans << endl; } return 0; }
相关文章推荐
- 京东2015年应届生招聘笔试题(A)卷答案选择题部分
- 京东2015年应届生招聘笔试题(A)卷答案选择题部分
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析1
- 编程题(2):京东-笔试题-2018实习招聘
- 2013年阿里巴巴暑期实习招聘笔试题目及部分答案——5月5日
- 2013年微软校园招聘笔试题及部分答案
- 阿里巴巴集团2016校园招聘-Python工程师笔试题(附加题+部分答案)
- 编程题(1):笔试题-京东软件测试2018实习招聘
- 京东2018年校园招聘笔试代码环节答案分享
- 中科创达2015校园招聘Java卷的笔试题及附加题答案
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析2
- 编程题(3):京东-笔试题-2018实习招聘
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析3
- 搜狐2010校园招聘第一站北邮笔试题(转) 以及 本人做的部分答案
- 迅雷校园招聘笔试题及答案解析(部分)
- 京东2017实习生招聘在线笔试编程题
- 京东2017实习生招聘在线笔试编程题题解
- 中兴2015校园招聘笔试题 +部分 答案(自己做的)
- 京东2017实习生招聘——在线笔试编程题总结
- 360公司2015年秋季校园招聘笔试考卷(技术类 D)部分试题程序验证和解析4