各大公司數據結構與算法面試題解答(一)

    還有一年就要找工作了,從現在開始找些公司的數據結構和算法的題來做一做,不定時貼出筆試面試題代碼。


1.創新工場:

求一個數組的最長遞減子序列比如{9,4,3,2,5,4,3,2}的最長遞減子序列爲{9,5,4,3,2}

算法描述:

1.      對原序列進行遞減排序(選擇快速排序法);

2.      刪掉重複數字;

3.      得到子序列。

C++源代碼:

#include "stdafx.h"

#include <iostream>

using namespace std;

#define MAX_SIZE 20

 

void FastSort(int a[],int left,int right)

{

       if(left< right)

       {

              inti = left,j = right,k;

              inttemp = 0;

              temp= a[left];

              while(i< j)

              {

                     while(j>i&&a[j]<temp)

                            j--;

                     a[i]= a[j];

                     while(i<j&&a[i]>= temp)

                            i++;

                     a[j]= a[i];

              };

              a[i]= temp;

              FastSort(a,left,i-1);

              FastSort(a,i+1,right);

       }

 

}

void GetChildSet(int a[],int p[],intn,int& length)

{

       length= 0;

       intcount = 0,i,j;

       inttemp[MAX_SIZE] = {0};

       for(j=0;j<n;j++)

              if(a[j]==a[j+1])

              {

                     temp[j+1]=1;

                     count++;

              }

       for(i=0,j=0;i<n,j<n-count;i++)

              if(0==temp[i])

              {

                     p[j]=a[i];

                     j++;

              }

       length= n-count;

}

 

int main(int argc, char* argv[])

{

       inta[MAX_SIZE] = {4,8,9,41,11,14,4,25,36,2,1,1,4,8,58,44,44,12,12,39};

       intlength = 0;

       FastSort(a,0,MAX_SIZE-1);

       intp[MAX_SIZE];

       GetChildSet(p,a,MAX_SIZE,length);

       for(inti=0;i<length;i++)

              cout<<p[i]<<endl;

       return0;

}

運行結果:

 

 

2將一整數序列逆序(要求遞歸實現)

算法描述:

1當滿足first<last時直接交換序列首位和末尾位元素;

2.遞歸調用(注意遞歸退出條件:必須是變量first大於或等於last)

C++源代碼:

#include "stdafx.h"

#include <iostream>

using namespace std;

#define MAX_SIZE 10

 

void Conver(int a[],int first,int last)

{

       inttemp = a[first];

       if(first== last||first > last)

              return;

       elseif(first<last)

       {

              a[first]= a[last];

              a[last]= temp;

       }

       Conver(a,first+1,last-1);

}

 

int main(int argc, char* argv[])

{

       inta[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};

       Conver(a,0,9);

       for(inti = 0;i < MAX_SIZE;i++)

              cout<<a[i]<<endl;

}

運行結果:

 

3將一整數逆序後放入一數組中(要求遞歸實現)如:12345逆置後爲54321

算法描述:

1分割出整數的每一個位,方法n%10;

2.每一次分割都是得到整數最後一個位,數組索引值要從第0開始

C++源代碼:

#include "stdafx.h"

#include <stdio.h>

void convert(int *result, int n)

{

 if(n>=10)

  convert(result+1, n/10);

 *result = n%10;

}

 

int main(int argc, char* argv[])

{

      int n = 123456789;

int *result = new int[10];

 convert(result, n);

 printf("%d:", n);

 for(int i=0; i<9; i++)

              printf("%d", result[i]);

printf("\n");

}

運行結果:

 

 

4 遞歸實現迴文判斷(如:abcdedbca就是迴文,判斷一個面試者對遞歸理解的簡單程序)

算法描述:

與求一個序列的轉置類似。

C++源碼

#include "stdafx.h"

#include <stdio.h>

bool Fun(char *s, int first,int last)

{

       if(first< last)

              if(s[first]== s[last])

              {

                     Fun(s,first+1,last-1);

              }

              else

                     returnfalse;

       elseif(first > last)

              returnfalse;

       else

              returntrue;

}

 

int main(int argc, char* argv[])

{

       char*s = "abcdedcba";

       if(Fun(s,0,8))

              printf("%sis plalindrome!",s);

       else

              printf("%sisn't plalindrome!",s);

       printf("\n");

}

運行結果:


5 華爲筆試

分解成質因數(如435234=251*17*17*3*2)

算法描述:

1 尋找一個整數的質因數方法從2開始遍歷,依次查找

C++源代碼:

#include "stdafx.h"

#include <stdio.h>

void Prime(int m,int n)

{

       if(n<m||n==m)

       {

              while(m%n!=0)

                     n++;

              m/= n;

              printf("%d*",n);

              Prime(m,n);

       }

       elsereturn;

}

 

int main(int argc, char* argv[])

{

       intn = 1000;

       printf("%d=",n);

       Prime(n,2);

       printf("\n");Prime(1000,2);

       printf("\n");

}

運行效果:

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章