英伟达2013年笔试题

1.给出下面常用的C变量的定义方式:

An array of 10 pointers to integers:int* p[10]

A pointer to an array of 10 integers int (*p)[10]

An array of ten pointers to functions that take an integer argument and return an integer: int (*p[10])(int)

2.Difference between semaphore and mutex;

互斥体:互斥体用于线程间的互斥,一次只允许一个线程进入临界区,它不能保证访问的顺序,因此其是无序访问。互斥体只能有同一线程释放,互斥体是睡眠锁,一旦资源被占用,资源的申请者只能进入睡眠状态,所以互斥体只能用于进程上下文,而不能用于中断上下文。

信号量:也称信号灯,进程/线程间同步用,保证对资源的顺序访问,一个进程(线程)完成了某一个动作就通过信号量告诉别的进程(线程),别的进程(线程)再进行某些动作,例如读者写者问题。信号量的释放可以由其他进程/线程释放.信号量为控制一个具有有限数量用户资源而设计,例如允许多个读者同时访问资源。同样信号量是睡眠锁不能用于中断上下文

自旋锁:类似于互斥体,但其可以用在中断上下文,因为对于没有获得锁的线程会循环检测,不会睡眠,但其占用cpu资源大。

信号量与互斥体的区别可以归结如下

1.      互斥体用于线程的互斥,信号量用于进程/线程的同步。

2.      互斥体必须由同一线程获得和释放对应使用,而信号量可以由一个线程释放,另一个线程得到

3.      互斥体的数值为0或1,信号量可以是任何非负整数


3. 100楼,2个eggs。

egg在第k楼及以上掉下会摔碎,k楼以下掉下不会碎。问如何确定k(K在1-100之间)

1个egg时肯定是从第1楼开始一层一层试;

那么2个eggs如何确定k?

3个eggs呢?

一般化:m个eggs,n个楼层,如何确定k?

Answer

Goal: Create a system for dropping Egg1 so that the most drops required is consistent, whether Egg1 breaks on the first drop or the last drop.
1. A perfectly load balanced system would be one in which Drops of Egg1 + Drops of Egg2 is always the same, regardless of where Egg1 broke.
2. For that to be the case, since each drop of Egg1 takes one more step, Egg2 is allowed one fewer step.
3. We must, therefore, reduce the number of steps potentially required by Egg2 by one drop each time. For example, if Egg1 is dropped on Floor 20 and then Floor 30, Egg2 is potentially required to take 9 steps. When we drop Egg1 again, we must reduce potential Egg2 steps to only 8. That is, we must drop Egg1 at floor 39.
4. We know, therefore, Egg1 must start at Floor X, then go up by X-1 floors, then X-2, …, until it gets to 100.
5. Solve for X+(X-1)+(X-2)+…+1 = 100. X(X+1)/2 = 100 -> X = 14
We go to Floor 14, then 27, then 39, … This takes 14 steps maximum.


4.100个门排成一排,开始时全部为closed。

第1次,           操作门1,2,3,4……100

第2次,           操作门2,4,6,8……100

第3次,           操作门3,6,9,12……99

第4次,           操作门4,8,12,16……100

……

第100次,操作门100

上面“操作”的意思时:如果原来门是open的,就关掉它;如果原来是closed的,就打开它。

 第100次之后,哪些门是open的,哪些门是closed的。


Answer: 看一个数有几个因子,因子数为奇数则为开,因子数为偶数极为关,每个数都可以表示为2个数的乘积,因此只要不是某个数的平方,即两两配对,为偶数个因子,若为某个数的平方必定为奇数个因子,所以1,4,9,16,25,36,49,64,81,100为open,其余为closed


5. 华为机试题:找出100以内的非素数

100=10*10;非素数必有一个因子小于10

因此只要能整除2,3,5,7即为非素数

不晓得是否还有更好的方法 请指教


你可能感兴趣的:(c,Semaphore,Integer,华为,Pointers)