一份C++軟件工程師的面試題

發布時間: 2009-11-20 17:31  閱讀: 4422 次  推薦: 0   [收藏]  

1、現有1000個蘋果,10個盒子,問各個盒子內應該分別放入多少個蘋果,才能使得用戶要買任意11000之間的一個蘋果數,都可以給他(賣的時候是整個盒子賣,不能拆盒子的包裝)。

2、請仔細閱讀下面的資料:

1)材料一:CArray
template< class TYPE, class ARG_TYPE > class CArray : public CObject
Parameters:
TYPE
Template parameter specifying the type of objects stored in the array.
TYPE is a parameter that is returned by CArray.
ARG_TYPE
Template parameter specifying the argument type used to access objects stored in the array.
Often a reference to TYPE. ARG_TYPE is a parameter that is passed to CArray.
Remarks:
The CArray class supports arrays that are are similar to C arrays, but can dynamically shrink and grow as necessary.
Array indexes always start at position 0. You can decide whether to fix the upper bound or allow the array to expand when you add elements past the current bound. Memory is allocated contiguously to the upper bound, even if some elements are null.

int CArray::Add (ARG_TYPE newElement);

Return Value:
The index of the added element.
Parameters:
ARG_TYPE
Template parameter specifying the type of arguments referencing elements in this array.
newElement
The element to be added to this array.

TYPE& CArray::operator [] (int nIndex);
Parameters:
TYPE
Template parameter specifying the type of elements in this array.
nIndex
Index of the element to be accessed.
Remarks:
Returns the array reference of element at the specified index.

2
)材料二:CList
template<class TYPE, class ARG_TYPE>class CList : public CObject
Parameters:
TYPE
Type of object stored in the list.
ARG_TYPE
Type used to reference objects stored in the list. Can be a reference.
Remarks:
The CList class supports ordered lists of nonunique objects accessible sequentially or by value.
CList lists behave like doubly-linked lists.

void CList::AddTail(ARG_TYPE newElement);
Parameters:
ARG_TYPE
Template parameter specifying the type of the list element (can be a reference).
newElement
The element to be added to this list.
Remarks:
Adds a new element or list of elements to the tail of this list. The list can be empty before the operation.

3
)材料三: realloc
realloc
Reallocate memory blocks.
void *realloc(void *memblock, size_t size);
Return Value:
The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.
Remarks:
The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.

4
)請指出下面這段代碼可能會出現的問題
CList<VARIANT*, VARIANT*> g_ValueList;
CArray<VARIANT, const VARIANT&> g_ValuePool;
void AddNewValue (const VARIANT& newValue)
{
g_ValueList.AddTail(&g_ValuePool[g_ValuePool.Add(newValue)]);
}

3
、有一無符號整型數組,大小為10, 初始的數值隨機,但在[0, 99]之間。請用C語言寫一個過濾程序,令數組內的數據互不相等。
說明:
1.若數組內有相等的數據,可令某一數值加1或減1作出偏移,直至不等為止。
2.數組內的數據只能在[0, 99]之間。
3.保持數組內的數據位置不變,即對應下標不變。

4 按要求編寫以下函數。
功能:將給定緩沖區中的#字符移到字符串尾部
函數名稱:ChangeToTail
入口參數:pSZ指向緩沖區的指針, nSize緩沖區長度
出口:pSZ所指緩沖區中的#字符被移到緩沖區尾部
返回值:在出口緩沖區中第一個#的位置,若緩沖區中無#字符則返回-1
說明:如傳入#W#W#W#WW# 10 則傳出時應轉換為WWWWW#####并且返回值為5
int ChangeToTail(BYTE* pSZ, UINT nSize)
{
// Todo
:請在此加入您的代碼
}

5
、在金山,有一個非常經典有趣的游戲,稱為殺人游戲。此游戲角色有:好人(m人)、壞人(n人)、村長(1人)、裁判(1人)。角色采用一定方式(如:抓鬮)分配。村長、裁判兩個角色是公開的,而好人、壞人兩個角色則只要裁判和本人心知肚明。其玩法如下:
游戲開始了,裁判說:“天黑了”,這是所有其他角色都低頭閉上眼睛(不準作弊!)。然后裁判說:“壞人開始活動”,此時壞人抬起頭,并相互商議,殺死一個好人。然后裁判說:“天亮了”,此時所有人抬頭,被殺死的那個好人宣布出局。剩下的人在村長的主持下,開始判斷殺人兇手。每個人可以根據各人的表情反應,判斷并提議殺死自己心目中的壞人。不過最終的裁決權屬于村長,綜合大家的意見殺死一人。此時裁判宣布此人出局。游戲進入下一輪,由天黑到天亮,再有2人出局,如此反復,直到最后好人先被全部殺死,則游戲結束,“邪惡”的一方戰勝了“正義”的一方;而另一個結局則是在大家以及村長的英明決斷下,壞人被全部殺死,則“正義”的一方戰勝了“邪惡”的一方。
現在,我們的問題是,請寫出你的思路,并編寫一個C/C++語言程序,求出:在壞人有兩個(n = 2)的情況下,需要多少個好人(m = ?),才能夠使這個游戲比較公平,也就是說,從概率上說,“正義”的一方與“邪惡”的一方勝利的幾率最接近于50%(此題與下面一題選做一道)

6、在以上的殺人游戲中,還有一個玩法,就是取消村長這個角色,“天亮”后剩余的人每個人投票列出自己心目中最懷疑的2個人,以此決定讓誰出局。試問,在此玩法下,上面的問題又何解?

7.C++程序設計
1.寫出以下程序的運行結果:

#include <iostream>
class Base
{
public:
Base()
{
cout << "Base()" << endl;
}
Base(const Base &theBase)
{
cout << "Base(const Base &theBase)" << endl;
}
~Base()
{
cout << "~Base()" << endl;
}
void Open()
{
OnOpen();
}
private:
virtual void OnOpen() = 0;
};

class Derived : public Base
{
public:
Derived()
{
cout << "Derived()" << endl;
}
Derived(const Derived &theDerived)
{
cout << "Derived(const Derived &theDerived)" << endl;
}
~Derived()
{
cout << "~Derived()" << endl;
}
private:
virtual void OnOpen()
{
//這里可能拋出異常
}
};

Base *CreateInstance()
{
return new Derived();
}

int main()
{
Base *pBase = ::CreateInstance();
if (pBase)
{
pBase->Open();
delete pBase;
}
return 0;
}

 

2.1)中,類Base和類Derived的實現有沒有問題?如果有,如何修改?

3.說明1)中類BaseOpen函數和OnOpen函數的設計目的和意義。

4.使用STL技術修改main()函數中的代碼,使之成為異常安全的。

 

0
0
 
標簽:面試題集
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()