创新工场2014笔试编程题答案
2013-09-18 13:19
330 查看
昨天笔试了创新工场的软件工程师,前面是10道选择题,后面是三道编程题。本人将三道编程题的程序贴出,供大家参考,如有错误敬请指正
PS:今天上午去了面试,准备了好多优化的算法的程序,但是都没用上,只问了一个归并排序就结束了,死的比较惨
编程题
1. 堆排序
2. 求一个正整数N的开方,要求不能用库函数sqrt(),结果的精度在0.001
解析:牛顿迭代
3. 给定一个矩阵intmaxtrixA[m]
,每行和每列都是增序的,实现一个算法去找矩阵中的某个元素element.
PS:今天上午去了面试,准备了好多优化的算法的程序,但是都没用上,只问了一个归并排序就结束了,死的比较惨
编程题
1. 堆排序
#include<iostream> usingnamespace std; void SwapValue(int &m, int &n) { int temp = m; m = n; n = temp; } void max_heap(vector<int> &vec, int i, int heap_size) { int l = 2*i; int r = 2*i+1; int largest = i; if(l<=heap_size && vec[l-1]>vec[largest-1]) largest = l; if(r<=heap_size && vec[r-1]>vec[largest-1]) largest = r; if(largest!=i) { SwapValue(vec[largest-1],vec[i-1]); max_heap(vec, largest, heap_size); } } void heapSort(vector<int> &vec) { int heap_size = vec.size(); for(int i=heap_size/2; i>=1; i--) max_heap(vec, i, heap_size); for(int i=heap_size; i>=1; i--) { SwapValue(vec[0],vec[i-1]); max_heap(vec, 1, i); } } void print(vector<int> vec) { for(int i=0; i<vec.size(); i++) cout<<vec[i]<<" "; cout<<endl; } int main() { vector<int> vec; vec.push_back(23); vec.push_back(5); vec.push_back(1); vec.push_back(10); vec.push_back(13); vec.push_back(32); vec.push_back(21); vec.push_back(14); vec.push_back(19); vec.push_back(20); cout<<"排序前: "<<endl; print(vec); heapSort(vec); cout<<"排序后: "<<endl; print(vec); return 0; }
2. 求一个正整数N的开方,要求不能用库函数sqrt(),结果的精度在0.001
解析:牛顿迭代
#include<iostream> using namespace std; int main() { int N; cout<<"输入N的值:"; cin>>N double x1 = 1;//初值 double x2 = x1/2.0+N/2.0/x1; while( fabs(x2-x1)>0.001) { x1 = x2; x2 = x1/2.0+N/2.0/x1; } cout<<x1<<endl; return 0; }
3. 给定一个矩阵intmaxtrixA[m]
,每行和每列都是增序的,实现一个算法去找矩阵中的某个元素element.
#include<iostream> using namespace std; const int M = 4; const int N = 4; int main { int matrix[M] = {}; double element; int flag = 1; for(int j=0; j<N; j++) { if(matrix[i][j] == element) cout<<"位置"<<endl; while( flag<M && matrix[i][j]<element ) --flag; while( flag<M && matrix[i][j]>element ) ++flag; } }
相关文章推荐
- 2014 微软校招笔试答案 编程题
- 笔试 - 2014创新工场涂鸦移动校园招聘测试题 及 答案
- 2014创新工场校招笔试题及参考答案
- 2014创新工场校招笔试题及参考答案
- 笔试 - 美团网2014校园招聘笔试试题 (哈尔滨站) 及 答案
- 完美世界2017年实习生游戏c++开发工程师笔试编程题答案
- 2014阿里巴巴9月14北京校园招聘笔试及参考答案
- 京东2015年应届生招聘笔试题(A)卷答案编程题和附加题部分
- 微软2014校园招聘笔试编程题
- 2014迅雷校园招聘笔试题答案
- 2014阿里巴巴9月15哈尔滨校园招聘笔试题及答案
- 2014迅雷校园招聘笔试题答案
- 2014创新工场校园招聘笔试题(9.16北京)
- 2014Microsoft 校招笔试真题(找工作的虾米们赶紧做题晒答案喽)
- 2014迅雷校园招聘笔试题答案
- 美团2014校招笔试题第二题答案分析
- 百度2014校园招聘研发工程师笔试题+答案
- 2014 腾讯 暑期实习生笔试题+大家可以说说自己答案。。
- 2014阿里巴巴校园招聘数据分析师职位笔试题目(答案版)
- 几道创新工场2014笔试题