筆試大雜燴 C++ (一)

數組a[N],存放了1至N-1個數,其中某個數重複一次。寫一個函數,找出被重複的數字.時間複雜度必須爲o(N)函數原型:int do_dup(int a[],int N)


一語句實現x是否爲2的若干次冪的判斷
  1. #include "stdio.h"  
  2.   
  3. int main(void)  
  4. {  
  5.       int b=16;  
  6.   
  7.       printf("%s\n",(b&(b-1)?"false":"ture") );  
  8.       return 0;  
  9. }  
Result:
ture

網易C++筆試題3. 一個類有基類、內部有一個其他類的成員對象,構造函數的執行順序是怎樣的?

  答:先執行基類的(如果基類當中有虛基類,要先執行虛基類的,其他基類則按照聲明派生類時的順序依次執行),再執行成員對象的,最後執行自己的。


網易C++筆試題6.請問下面程序有什麼錯誤?

  int a[60][250][1000],i,j,k;

  for(k=0;k<=1000;k++)

  for(j=0;j<250;j++)

  for(i=0;i<60;i++)

  a[i][j][k]=0;

  答案:把循環語句內外換一下

二維數組在c++中存儲,一般是按行存儲的,就是將一行當作一維數組進行存儲。
例如:a[2][2]這個二維數組,其在內存中存儲順序爲:
a[0][0] a[0][1]     //先存儲第一行
a[1][0] a[1][1]    //再存儲第二行。

假設是32位系統,整型佔4個字節,則其在內存的形式爲:
a[0][0]
a[0][1]
a[1][0]
a[1][1]

網易C++筆試題9.已知strcpy 函數的原型是:

  char *strcpy(char *strDest, const char *strSrc);

  其中strDest 是目的字符串,strSrc 是源字符串。不調用C++/C 的字符串庫函數,請編寫函數 strcpy

  答案:

  char *strcpy(char *strDest, const char *strSrc)

  {

  if ( strDest == NULL || strSrc == NULL)

  return NULL ;

  if ( strDest == strSrc)

  return strDest ;

  char *tempptr = strDest ;

  while( (*strDest++ = *strSrc++) != ‘’)

  ;

  return tempptr ;

  }


  網易C++筆試題10.寫一個函數找出一個整數數組中,第二大的數

  答案:

  const int MINNUMBER = -32767 ;

  int find_sec_max( int data[] , int count) //類似於1 4 4 4這樣的序列將認爲1是第二大數

  {

  int maxnumber = data[0] ;

  int sec_max = MINNUMBER ;

  for ( int i = 1 ; i < count ; i++)

  {

  if ( data[i] > maxnumber )

  {

  sec_max = maxnumber ;

  maxnumber = data[i] ;

  }

  else

  {

  if ( data[i] > sec_max )

  sec_max = data[i] ;

  }

  }

  return sec_max ;

  }


Linux網絡編程API函數初步剖析

 1socket(family,type,protocol)

1). 創建套接字:sock_create()

 2). 爲套接字綁定文件句柄:sock_map_fd()

2bind (sockfd, sockaddr, addrlen)

3listen(sockfd, backlog)

4connect(sockfd, sockaddr, addrlen)

5accept(sockfd, sockaddr, addrlen)

在網絡編程章節的數據接收過程中,我們主要介紹過read()、recv()、recvfrom()還有一個recvmsg()沒介紹到,今天我們就來看一下這幾個API函數到底有什麼差別

__sock_recvmsg()。

前面關於發送數據包時我們介紹過的API有write()、send()、sendto()還有一個sendmsg()沒介紹到。




①鏈表反轉

單向鏈表的反轉是一個經常被問到的一個面試題,也是一個非常基礎的問題。比如一個鏈表是這樣的: 1->2->3->4->5 通過反轉後成爲5->4->3->2->1。

最容易想到的方法遍歷一遍鏈表,利用一個輔助指針,存儲遍歷過程中當前指針指向的下一個元素,然後將當前節點元素的指針反轉後,利用已經存儲的指針往後面繼續遍歷。源代碼如下:

  1. struct linka {
  2. int data;
  3. linka* next;
  4. };
  5. void reverse(linka*& head) {
  6. if(head ==NULL)
  7.                   return;
  8. linka *pre, *cur, *ne;
  9. pre=head;
  10. cur=head->next;
  11. while(cur)
  12. {
  13.    ne = cur->next;
  14.    cur->next = pre;
  15.    pre = cur;
  16.    cur = ne;
  17. }
  18. head->next = NULL;
  19. head = pre;
  20. }

②已知String類定義如下:

class String
{
public:
String(const char *str = NULL); // 通用構造函數
String(const String &another); // 拷貝構造函數
~ String(); // 析構函數
String & operater =(const String &rhs); // 賦值函數
private:
char *m_data; // 用於保存字符串
};

嘗試寫出類的成員函數實現。

答案:

String::String(const char *str)
{
if ( str == NULL ) //strlen在參數爲NULL時會拋異常纔會有這步判斷
{
m_data = new char[1] ;
m_data[0] = '/0' ;
}
else
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}



String::String(const String &another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,other.m_data);
}


String& String::operator =(const String &rhs)
{
if ( this == &rhs)
return *this ;
delete []m_data; //刪除原來的數據,新開一塊內存
m_data = new char[strlen(rhs.m_data) + 1];
strcpy(m_data,rhs.m_data);
return *this ;
}


String::~String()
{
delete []m_data ;
}


printf("%s","abc""def");
結果是abcdef。
怎麼個原理?


你的另外一個帖子 都有人和你說了的 
這其實就是連接起來的一個字符串 

C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
    char *p = "hello" \
    "world";
   //或者
 
    char *p = "hello" 
    "world";
    printf(p);
//或者
 
    char *p = "hello""world";
    printf(p);
    printf(p);


求Fibonacci數列

遞歸法
long long Fibonacci_Solution1(unsigned int n)
{
      int result[2] = {0, 1};
      if(n < 2)
            return result[n];

      return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2);
}


更簡單的辦法是從下往上計算,首先根據f(0)f(1)算出f(2),在根據f(1)f(2)算出f(3)……依此類推就可以算出第n項了。很容易理解,這種思路的時間複雜度是O(n)

long long Fibonacci_Solution2(unsigned n)
{
      int result[2] = {0, 1};
      if(n < 2)
            return result[n];

      long long  fibNMinusOne = 1;
      long long  fibNMinusTwo = 0;
      long long  fibN = 0;
      for(unsigned int i = 2; i <= n; ++ i)
      {
            fibN = fibNMinusOne + fibNMinusTwo;

            fibNMinusTwo = fibNMinusOne;
            fibNMinusOne = fibN;
      }

 
      return fibN;
}


二叉樹層序遍歷


#include <deque>
#include <iostream>
using namespace std;

struct BTreeNode // a node in the binary tree
{
      int         m_nValue; // value of node
      BTreeNode  *m_pLeft;  // left child of node
      BTreeNode  *m_pRight; // right child of node
};

///////////////////////////////////////////////////////////////////////
// Print a binary tree from top level to bottom level
// Input: pTreeRoot - the root of binary tree
///////////////////////////////////////////////////////////////////////
void PrintFromTopToBottom(BTreeNode *pTreeRoot)
{
      if(!pTreeRoot)
            return;

      // get a empty queue
      deque<BTreeNode *> dequeTreeNode;

      // insert the root at the tail of queue
      dequeTreeNode.push_back(pTreeRoot);

      while(dequeTreeNode.size())
      {
            // get a node from the head of queue
            BTreeNode *pNode = dequeTreeNode.front();
            dequeTreeNode.pop_front();

            // print the node
            cout << pNode->m_nValue << ' ';

            // print its left child sub-tree if it has
            if(pNode->m_pLeft)
                  dequeTreeNode.push_back(pNode->m_pLeft);
            // print its right child sub-tree if it has
            if(pNode->m_pRight)
                  dequeTreeNode.push_back(pNode->m_pRight);
      }
}

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章