1. 程式人生 > >筆試面試之程式設計題

筆試面試之程式設計題

1.二分查詢

public int BinSearch(SeqList<int> R, int Key)
    {二分查詢
        int low = 0, high = R.GetLength() - 1, mid;//置當前查詢區間上、下界的初值
        while (low <= high)
        { //當前查詢區間R[low..high]非空
            mid = (low + high) / 2;
            if (R.Data[mid] == Key) return mid; //查詢成功返回
            if (R.Data[mid] > Key)
                high = mid - 1; //繼續在R[low..mid-1]中查詢
            else
                low = mid + 1; //繼續在R[mid+1..high]中查詢
        }
        return -1; //當low>high時表示查詢區間為空,查詢失敗
    }

2.程式設計實現strcmp

int MyStrcmp (const char *src, const  char *dst) 
{ 
	int   ret   =   0   ; 

	while( !(ret = *(unsigned   char   *)src   -   *(unsigned   char   *)dst)   &&   *dst) 
	{
		++src;
		++dst; 
	}
	if   (   ret   <   0   ) 
		ret   =   -1   ; 
	else   if   (   ret   >   0   ) 
		ret   =   1   ;
	return(   ret   ); 
}

3.函式實現判斷處理器是大端還是小端,若cpu是Big_endian,返回0;若是Little_endian,返回-1

//小端資料存放從低位元組到高位元組,大端資料存放從高位元組到低位元組
int checkCPU( )
{
          union w
           {  
                  int  a;
                  char b;
           } c;
           c.a = 1;

           return(c.b ==1);//c.b == 1為小段

}