一些自己曾经遇到过的笔试题

一些曾经遇到过的笔试题,比较混乱,不一定是出现在同一次笔试中的,只是为了记忆自己曾经的一些经历,也有益于以后的温习

1.编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来的是“abcdefghi”,如果n=2,移位之后应该是“hiabcdefgh”.

//pStr 是表示以‘\0’结尾的字符串的指针

//steps是表示要求移动的n

void LoopMove(char *pStr,int steps)

{

  //请填充

}

解析:这个试题主要考察面试者对于标准库函数的熟练程度,在需要的时候引用库函数可以很大程度的减少程序的编写工作量。
最被频繁使用的库函数包括strcpy,memcpy,memset
答案1:(使用strcpy)
void LoopMove(char* pstr, int steps)
{
  int n = strlen(pstr) - steps;
  char tmp[MAX_LEN];
  strcpy(tmp,pstr + n);
  strcpy(tmp+strps, pstr);
  *(tmp+strlen(pstr)) = '\0';
  strcpy(pstr,tmp);      
}

答案2:(使用memcpy)
void LoopMove(char* pstr,int steps)
{
  int n = strlen(pstr) - steps;
 char tmp[MAX_LEN];
  memcpy(tmp,pstr+n,pstr);
  memcy(pstr+steps,pstr,n);
  memcpy(pstr,tmp,steps);  

}
View Code

 2.请用递归和非递归方式分别实现二分法查找算法

递归:

int binaserach(int temp, int low, int high ,int arry[])
{
  if(low > high)
    rerurn -1;
   int min =(low + high)/2;
   if(arry[min] == temp) 
     return 1;
   if(arry[min] > temp)
     return binaserach(temp,low, min - 1,arry);
   if(arry[min] < temp)
     return binaserach(temp,min + 1,high,arry);
  return -1;                          
}
View Code


非递归:

int arry[] = {......};
int temp;
int low = 0;
int high = strlen(arry);
int count = 0;
while(1)
{
  count ++;
  if(count > strlen(arry))
    return -1;
  int min =(low+high)/2;
  if(arry[min] == temp)
    return 1;
  else if(arry[min] > temp)
    high = min -1;
  else if(arry[min] < temp)
    low = min + 1;                    
}
View Code

 

posted @ 2014-04-04 12:36  冬虫草。  阅读(167)  评论(0)    收藏  举报