创新工场2014笔试算法题汇总

来源:互联网 发布:linux下安装navicat 编辑:程序博客网 时间:2024/05/28 04:54

1. 堆排序

[cpp] view plaincopy
  1. #include<iostream>     
  2. usingnamespace std;     
  3.     
  4. void SwapValue(int &m, int &n)    
  5. {    
  6.     int temp = m;    
  7.     m = n;    
  8.     n = temp;    
  9. }    
  10. void max_heap(vector<int> &vec, int i, int heap_size)    
  11. {    
  12.     int l = 2*i;    
  13.     int r = 2*i+1;    
  14.     int largest = i;    
  15.         
  16.     if(l<=heap_size && vec[l-1]>vec[largest-1])    
  17.         largest = l;    
  18.     if(r<=heap_size && vec[r-1]>vec[largest-1])    
  19.         largest = r;    
  20.     
  21.     if(largest!=i)    
  22.     {    
  23.         SwapValue(vec[largest-1],vec[i-1]);    
  24.         max_heap(vec, largest, heap_size);    
  25.     }    
  26. }    
  27. void heapSort(vector<int> &vec)    
  28. {    
  29.     int heap_size = vec.size();    
  30.     for(int i=heap_size/2; i>=1; i--)    
  31.         max_heap(vec, i, heap_size);    
  32.     for(int i=heap_size; i>=1; i--)    
  33.     {    
  34.         SwapValue(vec[0],vec[i-1]);    
  35.         max_heap(vec, 1, i);    
  36.     }    
  37. }    
  38. void print(vector<int> vec)    
  39. {    
  40.     for(int i=0; i<vec.size(); i++)    
  41.         cout<<vec[i]<<" ";    
  42.     cout<<endl;    
  43. }    
  44.     
  45. int main()    
  46. {    
  47.     vector<int> vec;    
  48.     vec.push_back(23);    
  49.     vec.push_back(5);    
  50.     vec.push_back(1);    
  51.     vec.push_back(10);    
  52.     vec.push_back(13);    
  53.     vec.push_back(32);    
  54.     vec.push_back(21);    
  55.     vec.push_back(14);    
  56.     vec.push_back(19);    
  57.     vec.push_back(20);    
  58.         
  59.     cout<<"排序前: "<<endl;    
  60.     print(vec);    
  61.         
  62.     heapSort(vec);    
  63.         
  64.     cout<<"排序后: "<<endl;    
  65.     print(vec);    
  66.     return 0;    
  67. }     


2.求一个正整数N的开方,要求不能用库函数sqrt(),结果的精度在0.001
解析:牛顿迭代

[cpp] view plaincopy
  1. #include<iostream>    
  2. using namespace std;    
  3. int main()    
  4. {    
  5.     int N;    
  6.     cout<<"输入N的值:";    
  7.     cin>>N    
  8.     
  9.     double x1 = 1;//初值    
  10.     double x2 = x1/2.0+N/2.0/x1;    
  11.     while( fabs(x2-x1)>0.001)    
  12.     {    
  13.         x1 = x2;    
  14.         x2 = x1/2.0+N/2.0/x1;    
  15.     }    
  16.     cout<<x1<<endl;    
  17.     
  18.     return 0;    
  19. }    


3.给定一个矩阵intmaxtrixA[m][n],每行和每列都是增序的,实现一个算法去找矩阵中的某个元素element.

解法一:

[cpp] view plaincopy
  1. #include<iostream>    
  2. using namespace std;    
  3.     
  4. const int M = 4;    
  5. const int N = 4;    
  6. int main    
  7. {    
  8.     int matrix[M][N] = {};    
  9.     double element;    
  10.         
  11.     int flag = 1;    
  12.     for(int j=0; j<N; j++)    
  13.     {    
  14.         if(matrix[i][j] == element)    
  15.             cout<<"位置"<<endl;    
  16.         while( flag<M && matrix[i][j]<element )    
  17.             --flag;    
  18.         while( flag<M && matrix[i][j]>element )    
  19.             ++flag;    
  20.     }     
  21. }    

解法二:

[cpp] view plaincopy
  1. bool Find(int *matrixA, int m, int n, int element)    
  2. {    
  3.     bool found = false;    
  4.     if(matrixA != NULL & m & n)    
  5.     {    
  6.         int i,j;    
  7.         i=0;j=n-1;    
  8.         while(i<m;j>=0)    
  9.         {    
  10.             if(maxtrixA[i*n+j] == element)    
  11.             {    
  12.                 found = true;    
  13.                 break;    
  14.             }    
  15.             else if(matrix[i*n+j]>element    
  16.                 --j;    
  17.             else    
  18.                 ++i    
  19.         }    
  20.     }    
  21. }   

转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12233289

原创粉丝点击