今天去参加了优酷的笔试,感触蛮深!基本上都是考的数据结构和算法的题目,而其他的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);
}
}