優酷筆試有感

 

       今天去參加了優酷的筆試,感觸蠻深!基本上都是考的數據結構和算法的題目,而其他的c/c++的基本知識點差不多都沒有涉及到,所以如果要去這個公司的話(或者這種類型的公司),可得改改策略,好好準備下數據結構方面的知識。看來做遊戲開發更是看着這方面的基本功,平時用到的知識點和其他類型的公司差異較大。  好了看看主要有哪些題目吧,呵呵~~:

1.求一個三十二位整數的二進制數中一的個數

int count_ones(unsigned a)
{
    a = (a & 0x55555555) + ((a >> 1) & 0x55555555);
    a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
    a = (a & 0x0f0f0f0f) + ((a >> 4) & 0x0f0f0f0f);
    a = (a & 0x00ff00ff) + ((a >> 8) & 0x00ff00ff);
    a = (a & 0x0000ffff) + ((a >> 16) & 0x0000ffff);

    return a;
}


2.水仙花數

int a=n%10;
int b=n/10%10;
int c=n/100;
if(a*a*a+b*b*b+c*c*c==n)

3.點和麪的關係

法向量是垂直屏幕的法線表示的向量
設平面法向量爲{A,B,C},平面與法向量的交點爲P0:(x0,y0,z0).
則平面上一點(x,y,z)與(x0,y0,z0)的向量必然與法線垂直。因此得出平面的點法式方程:
A(x-X0) + B(y-y0) + C(z-z0) = 0
將判斷點座標代入方程 滿足條件 則點在平面上。
另:若方程座標多項式>0,則在平面正面(法向量方向),反之在背面

註釋:
兩向量a * b 的長度爲:
||a ||   *   ||b||    * sin(thta)   //thta爲a與b的夾角


這樣 A(x-X0) + B(y-y0) + C(z-z0) = 0 a,b垂直
     A(x-X0) + B(y-y0) + C(z-z0) > 0 a在b方向
     A(x-X0) + B(y-y0) + C(z-z0) < 0 a不在b方向

4.定義一個單向鏈表,實現2個單鏈表的合併

//: Link_define
template <class T>
class ChainNode {

private:
    T data;
    ChainNode<T> *link;
};
template<class T>
class Chain {
public:
    Chain() {first = 0;}
    ~Chain() ;
    bool IsEmpty() const {return first == 0;}
    int Length() const;
    bool Find(int k, T& x) const;
    int Search(const T& x) const;
    Chain<T>& Delete(int k, T& x);
    Chain<T>& Insert(int k, const T& x);
    void Output(ostream& out) const;
private:
    ChainNode<T> *first; // 指向第一個節點的指針
}

兩個有向單鏈表合併爲一個
#include <stdio.h>
#include <malloc.h>

struct LNode
{ int data;
    struct LNode *next;
};

struct LNode *insert(struct LNode *head,int x,int i);
void display(struct LNode *head);
struct LNode *combine(struct LNode *head1,struct LNode *head2);

main()
{
    struct LNode *head1,*head2,*head3;

head1 = NULL;
    head1 = insert(head1,2,1);
    head1 = insert(head1,12,2);
    head1 = insert(head1,67,3);
    display(head1);
    printf("/n");


    head2 = NULL;
    head2 = insert(head2,8,1);
    head2 = insert(head2,20,2);
    head2 = insert(head2,33,3);
    head2 = insert(head2,35,4);
    display(head2);
    printf("/n");

head3 = combine(head1,head2);
display(head3);
}

struct LNode *insert(struct LNode *head,int x,int i)
{
    int j=1;
    struct LNode *s,*q;
    q=head;
    s=(struct LNode *) malloc ( sizeof(struct LNode) );
    s->data=x;

    if(i==1)
    {
        s->next=q;
        head=s;
    }
    else
    {
        while(q->next != NULL)
        {
            q=q->next;
            j++;
        }
        if(j==i-1)
        {
            s->next=q->next;
            q->next=s;
        }
        else
            printf("error! there is no position/n");
    }
    return(head);
}

void display(struct LNode   *head)
{
    struct LNode *q;
    q=head;
    if(q==NULL)  
        printf("this is a NULL/n");
    else
        if(q->next==NULL)
            printf("%d",q->data);
        else
        {
            while(q->next!=NULL)
            {
                printf("%d->",q->data);
                q=q->next;
            }
            printf("%d",q->data);
        }
}

struct LNode *combine(struct LNode *head1,struct LNode *head2)
{
int n;
struct LNode *p1,*p2,*p3,*head3;
head3=NULL;
p1=head1;
    p2=head2;
    p3=head3;

    if(p1 == NULL && p2 == NULL)
        printf("null!!!/n");
    else
if(p1 != NULL && p2 == NULL)
   return(head1);
else
   if(p2 != NULL && p1 == NULL)
    return(head2);
   else
   {   n=1;     
    while(p1 != NULL && p2 != NULL)
    {
     if(p1->data < p2->data)
     {
      head3=insert(head3,p1->data,n++);
      p1=p1->next;                                        
     }
     else
      if(p1->data > p2->data)
      {
       head3=insert(head3,p2->data,n++);
       p2=p2->next;
      }
      else
      {
       head3=insert(head3,p1->data,n++);
       p1=p1->next;
       p2=p2->next;
      }
    }
    while(p1 != NULL && p2 == NULL)
    {
       head3=insert(head3,p1->data,n++);
       p1=p1->next;
    }
    while(p2 != NULL && p1 == NULL)
    {
       head3=insert(head3,p2->data,n++);
       p2=p1->next;
    }
   }
   return(head3);
}

5.迷宮算法

這裏寫出主要實現功能:

void Print(Point *head) /*打印行進路徑*/
{
   Point *r;
  
   r = head;
   while(r->next != NULL)
   {
      printf("(%d,%d)-> ", r->x, r->y);
      r = r->next;
   }  
   printf("(%d,%d)", r->x, r->y);
}  

void Store(Point *head)
{
   int i=0, flag=1;
   char *s;
  
   while(flag)
   {
      if(head->next != NULL)
      {
         a[row][i++] = '(';
         a[row][i++] = (char)(head->x + 48);
         a[row][i++]   = ',';
         a[row][i++]   = (char)(head->y + 48);
         a[row][i++]   = ')';
         a[row][i++] = '-';
         a[row][i++] = '>';
         a[row][i++] = ' ';
         head = head->next;
      }
      else
       flag = 0;  
   }  
   a[row][i++] = '(';
   a[row][i++] = (char)(head->x + 48);
   a[row][i++] = ',';
   a[row][i++] = (char)(head->y + 48);
   a[row][i++] = ')';
   a[row][i++] = '/0';
   row++;
}

void Pai_Xu()
{
   int i, j, min;
   char p[200];
  
   for(i=0; i<row-1; i++)
   {
      min = i;
      for(j=i+1; j<row; j++)
         if(strlen(a[min]) > strlen(a[j]) )
          min = j;
      if(min != i)
      {
         strcpy(p, a[i]);
         strcpy(a[i], a[min]);
         strcpy(a[min], p);
      }
   }         
}

void Xiao_Chu()
{
   int i, j;
   char p[200]="fail";
  
   for(i=0; i<row-1; i++)
    for(j=i+1; j<row; j++)
         if(strcmp(a[i], a[j]) == 0)
          strcpy(a[j], p);
}

void Print_Total()
{
   int i;
  
   puts("/nthe total root:");
   for(i=0; i<row-1; i++)
    if(strcmp(a[i], "fail") != 0)
       puts(a[i]);
}

void Free(void)
{
   Point *r;
  
   while(head)
   {
      r = head;
      head = head->next;
      free(r);
   }  
}
             

還有幾道好像是是關於何時用到複製構造函數的選擇題,平時感覺這種知識點學挺好的,今天一下就懵了,看來還是要好好挖挖啊~~;和關於迴文的一道提空題,以及一道屏幕顯示找錯題。一共就是這8道了,希望對以後去筆試的有所幫助哦。

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