创新工场笔试题2013年校园招聘 .

2013-09-06 10:46:16

转自:CSDNhuangxy10的专栏

时间:2012年9月27日 地点:鼎好大厦10层

考试时长:1小时

 

 

一,      选择题

1,求z的结果

 

  1. #define N 3   
  2. #define Y(n) ((N+1)*n)   
  3. z = 2*(N+Y(5+1));  
#define N 3
#define Y(n) ((N+1)*n)
z = 2*(N+Y(5+1));

解答:48

 

2,有关多线程,多进程的描述错误的是

A,       子进程获得父进程的数据空间,堆和栈的复制品

B,       线程可以与同进程的其他线程共享数据,但是它拥有自己的栈空间且拥有独立的执行序列

C,       线程执行开销小,但是不利于资源管理和保护

D,       进程适合在SMP机器上进行,而线程则可以跨机器迁移

解答:D

 

3,

 

  1. struct s  
  2. {  int x:3;  
  3.    int y:4;  
  4.    int z:5;  
  5.  double a;  
  6. }  
struct s
{  int x:3;
   int y:4;
   int z:5;
 double a;
}

 

求sizeof(s)

解答:

16

:是取位的作用,前三个变量是为两个字节,最后double变量是8个字节,

结构体以8字节对齐,则为16字节。

 

4,序列{2,1,4,9,8,10,6,20}是某排序算法第二轮排序的结果,则该算法只能是

A快速排序    B冒泡排序

C选择排序    D插入排序

解答:A

 

5,我们需要监听一个事件状态,让它在状态发生改变时主动发出通知,请问需要哪种设计模式?

A装饰者模式 B建造者模式

C创新工场模式 D观察者模式

解答:D

 

6,有2012瓶矿泉水,其中有一瓶有毒,请问需要多少只老鼠才能一次性找到有毒的矿泉水?

解答:11只

 

二,      问答题

1,       有0-n这n+1个数,但是其中丢了一个数,请问如何找出丢了哪个数?

解答:

求这n个数的sum,然后计算n(n+1)/2-sum可得。

 

2,       解释  

  1. #typedef char (*func)(int,char*)  
#typedef char (*func)(int,char*)

解答:

定义了一个函数指针的数据类型;

该数据类型可以用来定义函数指针;

定义的函数指针指向的函数的参数为

  1. (int,char*)  
(int,char*)

 

返回值为char型。

 

 

3,       求输出结果

 

  1. int a[2][2][3]= { {{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};  
  2. int *ptr=(int *)(&a+1);  
  3. printf(“%d %d”, *(int*)(a+1), *(ptr-1));  
int a[2][2][3]= { {{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}};
int *ptr=(int *)(&a+1);
printf(“%d %d”, *(int*)(a+1), *(ptr-1));

解答:

 7  12 (已修定)

考察多级指针,一定要明确指针指向的是什么,才能知道它加1后跳过了多少字节。

&a是个四级指针,指向的是a这样的数组,所以它加1,就会跳过整个数组。

 

 

4,求输出结果

 

  1. #include <iostream>   
  2. using namespace std;  
  3. class A  
  4. {  
  5. public:  
  6.     virtual void print()  
  7.     { cout << "A::print()" <<endl;}  
  8. };  
  9. class B: public A  
  10. {  
  11. public:  
  12.     virtual void print()  
  13.     { cout << "B::print()" <<endl;}  
  14. };  
  15. class C: public A  
  16. {  
  17. public:  
  18.     virtual void print()  
  19.     { cout << "C::print()" <<endl;}  
  20. };  
  21. void print(A a)  
  22. {  
  23.     a.print();  
  24. }  
  25. void main()  
  26. {  
  27.     A a,*aa,*ab,*ac;  
  28.     B b;  
  29.     C c;  
  30.     aa=&a;  
  31.     ab=&b;  
  32.     ac=&c;  
  33.     a.print();  
  34.     b.print();  
  35.     c.print();  
  36.     aa->print();  
  37.     ab->print();  
  38.     ac->print();  
  39.     print(a);  
  40.     print(b);  
  41.     print(c);  
  42. }  
#include <iostream>
using namespace std;
class A
{
public:
	virtual void print()
	{ cout << "A::print()" <<endl;}
};
class B: public A
{
public:
	virtual void print()
	{ cout << "B::print()" <<endl;}
};
class C: public A
{
public:
	virtual void print()
	{ cout << "C::print()" <<endl;}
};
void print(A a)
{
	a.print();
}
void main()
{
	A a,*aa,*ab,*ac;
	B b;
	C c;
	aa=&a;
	ab=&b;
	ac=&c;
	a.print();
	b.print();
	c.print();
	aa->print();
	ab->print();
	ac->print();
	print(a);
	print(b);
	print(c);
}

解答:

A::print();

B::print();

C::print();

 

A::print();

B::print();

C::print();

A::print();

A::print();

A::print();

 

 

三,算法编程题

1,有1分,2分,5分,10分四种硬币,每种硬币数量无限,给定n分钱,求有多少种组合可以组合成n分钱?

解答:

思路: 

①,四层循环

②,使用回溯法在空间中搜索

代码为思路2:

 

  1. // chuangxingongchan.cpp : 定义控制台应用程序的入口点。   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5. #include <vector>   
  6. #include <iostream>   
  7. using namespace std;  
  8.   
  9. int count=0;  
  10. int Target=0;  
  11.   
  12. int coin[4]={1,2,5,10};  
  13. int total=0;  
  14. vector<int> solution;  
  15.   
  16. void dfs(int index)  
  17. {  
  18.     if( total == Target )  
  19.     {  
  20.         count++;  
  21.         cout << count <<":" ;  
  22.         forint i=0; i<(int)solution.size(); i++)  
  23.         {  
  24.             cout  << solution[i]<<" ";  
  25.         }  
  26.         cout << endl;  
  27.         return;  
  28.     }  
  29.   
  30.     if( total > Target )  
  31.         return;  
  32.   
  33.     forint i=index; i<4; i++)  
  34.     {  
  35.         total += coin[i];  
  36.         solution.push_back( coin[i] );  
  37.         dfs(i);  
  38.         solution.pop_back();  
  39.         total -=coin[i];  
  40.     }  
  41. }  
  42.   
  43. int _tmain(int argc, _TCHAR* argv[])  
  44. {  
  45.     while(1)  
  46.     {  
  47.         count=0;  
  48.         cin >> Target;  
  49.         dfs(0);  
  50.         cout << count <<endl;  
  51.     }  
  52.     return 0;  
  53. }  
// chuangxingongchan.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int count=0;
int Target=0;

int coin[4]={1,2,5,10};
int total=0;
vector<int> solution;

void dfs(int index)
{
	if( total == Target )
	{
		count++;
		cout << count <<":" ;
		for( int i=0; i<(int)solution.size(); i++)
		{
			cout  << solution[i]<<" ";
		}
		cout << endl;
		return;
	}

	if( total > Target )
		return;

	for( int i=index; i<4; i++)
	{
		total += coin[i];
		solution.push_back( coin[i] );
		dfs(i);
		solution.pop_back();
		total -=coin[i];
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	while(1)
	{
		count=0;
		cin >> Target;
		dfs(0);
		cout << count <<endl;
	}
	return 0;
}

 

 

 

2,马戏团里有个叠罗汉的表演,为了便于美观,下面的人身高和体重都要大于上面的人。现在知道n个演员的身高和体重,请问最多能叠多少层?

解答:

思路:

首先生成一个有向图,用连接矩阵的方式来表示。

map[i][j]==1表示第i个人上面可以放第j个人。

然后开始对每个人进行深度搜索,这个图中不可能有环。

所以对于每个人来说就是一棵树,搜索树的高度。

再找出最高的高度即是答案。

 

    1. #include "stdafx.h"   
    2. #include <iostream>   
    3. #include <fstream>   
    4. #include <vector>   
    5. #include <cstdlib>   
    6. using namespace std;  
    7.   
    8. int N=0;  
    9. double *weight;  
    10. double *height;  
    11.   
    12. int **map;  
    13. int maxDepth=0;  
    14.   
    15. vector<int> bestPath;  
    16.   
    17. int dfs( int index, vector<int> &path )  
    18. {  
    19.     int flag=0;  
    20.     int depth = 0;  
    21.     vector<int> bestPath;  
    22.     forint i=0; i<N;i++)  
    23.     {  
    24.         if( map[index][i] != 0)  
    25.         {   
    26.             flag = 1;  
    27.             vector<int> tPath;  
    28.             int t = dfs(i, tPath);  
    29.             if( t > depth )  
    30.             {  
    31.                 path = tPath;  
    32.                 depth = t;  
    33.             }  
    34.         }  
    35.     }  
    36.   
    37.     if( flag==0 )  
    38.     {     
    39.         path.clear();  
    40.         path.push_back(index);  
    41.         return 1;  
    42.     }  
    43.     else  
    44.     {  
    45. //      path = bestPath;   
    46.         path.push_back(index);  
    47.         return depth+1;  
    48.     }  
    49. }  
    50.   
    51. void CreateMap()  
    52. {  
    53.     map = new int*[N];  
    54.   
    55.     forint i=0; i<N; i++)  
    56.     {  
    57.         map[i] = new int [N];  
    58.         memset( map[i], 0, N*sizeof(int) );  
    59.     }  
    60.   
    61.     forint i=0; i<N; i++)  
    62.     {  
    63.         forint j=0; j<N; j++)  
    64.         {  
    65.             if( weight[j]<weight[i] && height[j]<height[i] )  
    66.                 map[i][j]=1;  
    67.         }  
    68.     }  
    69. }  
    70.   
    71. void CreateData()  
    72. {  
    73.     ofstream out( "in.txt" );  
    74.     int N = 30;  
    75.     out << N <<endl;  
    76.     forint i=0; i<N; i++)  
    77.         out << rand() << " ";  
    78.     out << endl;  
    79.   
    80.     forint i=0; i<N; i++)  
    81.         out << rand() << " ";  
    82. }  
    83.   
    84. int main()  
    85. {  
    86.     CreateData();     
    87.     freopen( "in.txt""r", stdin );  
    88.     cout << "Please input N:" <<endl;  
    89.     cin >> N;  
    90.     height = new double[N];  
    91.     weight = new double[N];  
    92.     forint i=0; i<N; i++)  
    93.         cin >> height[i];  
    94.     forint i=0; i<N; i++)  
    95.         cin >> weight[i];  
    96.   
    97.     CreateMap();  
    98.     int depth=0;  
    99.     for(int i=0; i<N;i++)  
    100.     {  
    101.         vector<int> tPath;  
    102.         int t=dfs(i,tPath);  
    103.         if( t>depth )  
    104.         {  
    105.             bestPath = tPath;  
    106.             depth = t;  
    107.         }  
    108.     }  
    109.     cout << depth <<endl;  
    110.     forint i=0; i<(int)bestPath.size(); i++)  
    111.     {  
    112.         cout << height[bestPath[i]]<< " " << weight[bestPath[i]]<<endl;  
    113.     }  
    114.     return 0;  
    115. }
posted @ 2013-09-06 10:47  永不止步,永无止境  阅读(212)  评论(0)    收藏  举报