- 相關(guān)推薦
c++一些筆試題目和整理的答案
NO1
Below is usual way we find one element in an array
const int *find1(const int* array, int n, int x)
{
const int* p = array;
for(int i = 0; i < n; i++)
{
if(*p == x)
{
return p;
}
++p;
}
return 0; }
In this case we have to bear the knowledge of value type "int", the size of array, even the existence of an array. Would you re-write it using template to eliminate all these dependencies?
template
const T *find1(const T* array, int n, T x)
{
const T* p = array;
for(int i = 0; i < n; i++)
{
if(*p == x)
{
return p;
}
++p;
}
return 0; }
NO2
Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
template
class Stack
{
public:
Stack(int = 10) ;
~Stack() { [] stackPtr ; }
int push(const T&);
int pop(T&) ;
int isEmpty()const { return top == -1 ; }
int isFull() const { return top == size - 1 ; }
private:
int size ; // number of elements on Stack.
int top ;
T* stackPtr ;
} ;
NO3
Implement the simplest singleton pattern(initialize if necessary).
class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
}
// Implementation
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if (_instance == 0) {
_instance = new Singleton;
}
return _instance;
}
NO4
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
1)Totally 10 coins
2)One can take away 1,2or 4 coins at one time by turns
3)Who takes the last loses.
Given these rules Whether the winning status is pre-determined or not
解答: 1:從后面開(kāi)始考慮,最后肯定要留1個(gè)才能保證自己贏
2:所以要設(shè)法讓對(duì)方留下2,3,5個(gè)
3:也就是要自己取后留下1,4,6,7,8,9
4:如果自己取后留下6,對(duì)方取2個(gè),與(3)矛盾,所以排除6
5:如果自己取后留下8,對(duì)方取4個(gè),與(3)一樣情況,所以也排除8
6:同樣,9也不行,如果我抽后剩下9,對(duì)方抽2個(gè),就反過(guò)來(lái)成對(duì)方抽剩成7個(gè)了,也與3)矛盾,所以也排除
7:所以很顯然,我只能抽剩1,4,7
8:因?yàn)橹荒艹楹笫?,4,7才能贏,我先抽得話不可能達(dá)到這幾個(gè)數(shù),很顯然,只能讓對(duì)
方先抽,也即是先抽的人輸
【c++一些筆試題目和整理的答案】相關(guān)文章:
報(bào)社筆試題目及答案03-18
史上最全軟件筆試題目及答案08-21
民生銀行筆試題目及答案03-17
幼師招聘筆試題目04-02
廣發(fā)銀行筆試題目08-19
醫(yī)院護(hù)士招聘筆試題目08-21
羊城晚報(bào)筆試題目04-11
出版社筆試題目04-08
唯品會(huì)校園招聘筆試題目10-09
外貿(mào)公司面試筆試題目03-11