1. 程式人生 > >2019年校招:縱目科技演算法筆試題

2019年校招:縱目科技演算法筆試題

參考

當時筆試感覺良好,但是遺憾沒有接到面試通知,把筆試題目寫出來給大家參考參考。試卷分為兩部分,第一部分為必做題,第二部分選做兩題

Settion 1: SW Program Questions
1、Implement the following macro to clear a 32 bit register’s bit 4,5 and 6

思路:題目要求清空二進位制數指定位的值,找一個數:需要清除的位置置零,其他位置為1的數。將兩個數作與操作。

參考:

#define ClrBit456(x) ((x)&0x‭FFFFFFC7‬)

2、What is problem of the following logic and how to modify it to make it work correctly?

void allocate_mem(char *str){
    Str = (char*) malloc(100);
}
void test(){
    char*str = NULL;
    allocate_mem(str);
    strcpy(str, "Hello World");
    printf(str);
}

思路:指標申請一塊記憶體,然後將字串拷貝到申請的記憶體中去,然後輸出字串。定義字串,然後將指標指向定義的記憶體塊,需要傳地址方式或者引用方式,然後將字串拷貝進指標所指向的記憶體塊中。最後根據指標輸出字串。

參考答案:

void allocate_mem(char **str) {//注意str為二級指標
    *str = (char*)malloc(100);//等號右側為指標型別,左側也要指標型別
}
void test() {
    char *str = NULL;
    allocate_mem(&str);//引用方式傳遞
    strcpy(str, "Hello Word");
    printf(str);
    free(str);//最後不要忘記釋放記憶體
}

3、Please list the system output

unsigned char a;
unsigned short b;
unsigned int c;
unsigned long d;
unsigned long long e;
unsigned int* p1, p2;
cout<< sizeof(a) << " " << sizeof(b) " " << sizeof(c) << " " << sizeof(d) << " " 
    << sizeof(e) << " " << sizeof(p1) << " " << sizeof(p2);

Complied and run by 32bit complier Complied and run by 64bit complier

思路:本題目考察關於32位和64位系統中常見變數的儲存長度。為了跨平臺的相容性,常見的變數長度都不變,變化的為:* 指標(4/8)、long(4/8)、unsigned long(4/8)。之所以p2不變是因為兩個地方都是int而不是int*

參考:

complier char short int long long long int* p1 p2
32 1 2 4 4 8 4 4
64 1 2 4 8 8 8 4

4、Please list the system output: TODO

unsigned char a = 128;
unsigned int b = ( a << 8 ) & ( a++ ) || (++a);

思路:首先分析unsigned char a=128;二進位制為:1000 0000; a<<8左移8位溢位,和a作與操作,結果為0,0再和++a作或,因為a非0,結果為1。b=1;因為a自加了兩次,故由128變為130;

a=130
b=1

5、有一隻貓在半徑為r的圓周上以速度為v移動,貓只能在圓周上移動, 但可以自由改變方向。 圓心位置有一隻老鼠。 老鼠可以在圓內自由移動, 但速度僅為 v/4。 老鼠按照什麼樣的策略/路線可以逃逸圓周而不被捕獲。

思路: 1首先計算直接朝外跑,看老鼠跑R這麼遠,貓在同樣時間能否比半個圓周短,如果老鼠跑R的時間,貓跑在同樣的時間內跑的超過半個圓周,那麼行不通。其實毫無疑問是行不通的,不然題目就沒有意義了。 2、我們知道,老鼠雖然速度慢,但是如果老鼠圍繞著內圈跑,貓繞著外圈跑,不一定會比貓慢(弧度)。可以找到二者跑同樣的弧度的情況下,看此時老鼠離圓心多遠,然後看此時再直接朝圓外跑,能不能跑出去,經過計算式可以跑出去的。

Settion 2: SW Program Questions
1、implement the following 2 functions.
  • Use the ANSI malloc and free functions for actual mermory allocation.
  • Pointer returned by Alloc64Aligned shall be 64 byte aligned.
  • Do not use global variables in your logic.
void * Alloc64Aligned (int nSize)
void Free64Aligned ( void * p )

思路: 不讓自己寫記憶體分配函式;Alloc64Aligned返回的指標必須是64位元組對齊;不讓用全域性變數;

參考程式碼:

void * Alloc64Aligned (int nSize){
	char *ret;
	int block; 
	int offset;
	int i;
	if (size<=0){
		return NULL;
	}
	block=1+size/8; 
	ret=(char *)malloc(block*8);
	offset=((unsigned long)ret)%8;
	ret[0]=0;
	for (i=1;i<8-offset;++i){
		ret[i]=1;
	}
	return ret+8-offset;
}
void Free64Aligned ( void * p ){
	char *buf;
	int i;
	if (p==NULL){
		return ;
	}
	buf=(char *)p;
	for (i=1;i<8;++i){
		if (*(buf-i)==0){
			break;
		}
	}
	free(buf-i);
}

2、實現一個函式,計算兔子的數量 有一對兔子,從出生後第3個月起每生一對兔子,小兔子長到第三個月後又生一對兔子,假設兔子都不死,問第n個月兔子的總數為多少?

思路:經典的斐波那契數列問題

int CalculateRabbitCount(int Num){
	int temp[32],i;
	temp[0]=1,temp[1]=1;
	for (i=2;i<Num;++i)
		temp[i]=temp[i-1]+temp[i-2];
	return temp[Num-1];
}

3、實現以下 random_sort 的函式, 對輸入的陣列做隨機排序 rand函式是已知庫函式, 可以返回0到1之間的一個浮點數;array 是輸入的一個遞增陣列, size是陣列的長度。 函式返回時的結果也存在array的陣列中。

float rand(); // return random value, 0 <= random value < 1
void random_sort(int* array, int size)

參考程式碼:

void random_sort(int* array, int size){
	int i,p,e;
	for (i=0;i<size-1;++i){
		p=(int)(rand()*(size-i));
		e=array[i];
		array[i]=array[i+p];
		array[i+p]=e;
	}
}

4、實現以下程式,對一幀8位灰度圖實現雙線性拉伸 src_bud是傳入影象, dst_buf是傳出影象, src_width, srdc_height 是傳入影象的寬高, dst_width, dst_height 是傳出影象的寬高 雙線性拉伸的定義: 當需要計算P 點的值的時候, P點的值等於分別到最臨近的 A, B, C, D四個點的值乘以距離的加權平均。

void bilinear_interpotation(unsigned char* src_buf,
                            int src_width,
                            int src_height,
                            unsigned char* dst_buf,
                            int dst_width,
                            int dst_height)

參考程式碼: