創新工場2014筆試演算法題匯總

來源:互聯網
上載者:User


#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;  }   


#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;  }  


#include<iostream>  using namespace std;    const int M = 4;  const int N = 4;  int main  {      int matrix[M][N] = {};      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;      }   }  

解法二:

bool Find(int *matrixA, int m, int n, int element)  {      bool found = false;      if(matrixA != NULL & m & n)      {          int i,j;          i=0;j=n-1;          while(i<m;j>=0)          {              if(maxtrixA[i*n+j] == element)              {                  found = true;                  break;              }              else if(matrix[i*n+j]>element                  --j;              else                  ++i          }      }  } 




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.