阿爾卡特筆試題目之一

來源:互聯網
上載者:User
1、能否實現一個宏來實現sizeof(type),   sizeof(var),   type為基本類型,var   為變數.  
  #define     NEWSIZEOF(x)     sizeof(x)       這樣答題是不會得分的。回答不能實現,也是0分。   // selfsizeof.cpp : 定義控制台應用程式的進入點。
//

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

#define NEWSIZEOFVAR(x)     ((char*)(&x+1)-(char*)&x) 

int _tmain(int argc, _TCHAR* argv[])
...{
    int rhs = 0;
    int result = NEWSIZEOFVAR(rhs);
    cout << "the result is " << result;
    return 0;
}

3、結構體sizeof   的題目。不寫了。最後一問:   結構體成員能否進行   16bytes對齊?  
  根據編譯器和OS,   在64位OS上結構體內的資料成員可能16bytes   對齊. http://www.vckbase.com/document/viewdoc/?id=1054;http://blog.vckbase.com/panic/archive/2005/04/02/4340.aspx

4、寫一個函數reverse一個byte  
  unsigned   char   ByteReverse(unsigned   char   szByte)  
  {    
      unsigned   char   a   =   szByte,    
      unsigned   char   b   =   0;  
      int   i;  
      for(i=0;   i<8;   i++) {  
      b   <<=   1;  
      b   |=   (0x01   &   a);  
      a   >>=   1;  
      }  
      return   b;  
  }   
   
5、寫一個函數判斷一個整型是不是power   of   2  
  bool   IS_POWEROFTWO(unsigned   int   m)  
  {   
      if(   ((m&(m-1))==0)   &&   m>0   )   //decrease by 1 to remove one "1" in the binary of num at once.
      return   true;  
      else  
      return   false;  
  }   
   
7、2選1  
  寫個函數實現反轉一個字串的功能        

// stringReverse.cpp : 定義控制台應用程式的進入點。
#include "stdafx.h"
#include <iostream>
using namespace std;

/************************************************************************/  
//   函數名稱:   Ustrlen  
//   輸入參數:   strSource,待求長度字串;  
//   輸出參數:   int,字串的長度。  
//   描         述:   通過判斷字元'/0'來得到字串的長度  
/************************************************************************/    
int   Ustrlen(const   char   *strSource)  
{  
 //   聲明變數  
 int   iLength(0);  
 //   遍曆字串,尋找字元'/0'  
 while(*strSource++   !=   '/0')  
 {  
  ++iLength;  
 }  
 //   返回字串的長度  
 return   iLength;  
}  
/************************************************************************/  
//   函數名稱:   _ReversalChar  
//   輸入參數:   strSouce,待反轉字串;iStart,旋轉字串開始位置;iEnd,旋轉字串結束位置  
//   輸出參數:   char*,反轉後字串的指標;  
//   描         述:   反轉iStart到字串iEnd之間的字串  
/************************************************************************/  
char*   _ReversalChar(char   *strSouce,int   iStart,int   iEnd)  
{  
 //   反轉字串  
 for(;iEnd   >   iStart;   ++iStart,--iEnd)  
 {  
  char   ch;  
  ch   =   strSouce[iStart];  
  strSouce[iStart]   =   strSouce[iEnd];  
  strSouce[iEnd]   =   ch;  
 }  
 //   返回字串指標  
 return   strSouce;  
}  

/************************************************************************/  
//   函數名稱:   ReversalChar  
//   輸入參數:   strSource,待反轉字串  
//   輸出參數:   char*,反轉字串後的指標  
//   描         述:   按單詞反轉字串  
/************************************************************************/  
char   *   ReversalChar(char   *strSouce)  
{  
 //   擷取字串的長度  
 int   iLength   =   Ustrlen(strSouce);  

 //   反轉整個字串  
 _ReversalChar(strSouce,0,iLength-1);  

 //   聲明變數(單詞的開始以及結束預設從0開始)  
 int   iStart(0),iEnd(0);  

 //   尋找單詞  
 //   像上面討論的尋找單詞的情況,我們只需要修改這部分,就可以實現對不  
 //   同格式類型單詞進行處理,為了更好的通用性,其實最好把尋找單詞這部分  
 //   作為單獨一個函數,或者一個類來處理  
 for(int   i   =   0;   i   <   iLength;   ++i)  
 {  
  //   尋找空格分割符號  
  if(strSouce[i]  ==   ' ')  
  {  
   //   找到一個單詞  
   iEnd   =   i-1;  
   //   對於只有一個字元的單詞比如說(I)沒有必要反轉  
   if(iStart   <   iEnd)  
   {  
    //   反轉單詞  
    _ReversalChar(strSouce,iStart,iEnd);  
   }  
   //   記錄下一個單詞的開始位置  
   iStart   =   i+1;  
  }  
  //   特殊處理幾種常見標點符號  
  else   if(strSouce[i]   ==   '!'   ||   strSouce[i]   ==   ','   ||   strSouce[i]   ==   '.')  
  {  
   iStart   =   i+1;  
  }  
 }  
 //   返回反轉後的字串  
 return   strSouce;  
}  

int main ()
{
 char  str []= "I want to be get hired!!!";
 cout << "before reverse " << str << endl;
 cout << "after reverse " << ReversalChar(str) << endl;
 return 0;
}

8、不另外增加儲存空間實現如下功能:   a=1,   b=2,   交換a和b的值   
public   int   swap (int   x,int   y){
      x   =   x   +   y;
      y   =   x   -   y;
      x   =   x   -   y;


9、改錯題  
  void   f()  
  {  
  unsigned   static   int   i;  
  int   sum   =   0;  
  for(i=0;   i<=100;   i++)  
  sum+=i;  
          return   sum;  
  }  
  去掉static,否則每次調用函數得到的結果都不同,static變數會保留值. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.