一些笔试题目和整理的答案 - 腾讯(Tencent)
来源:互联网 发布:centos 6.7 破解密码 编辑:程序博客网 时间:2024/06/05 08:44
一些笔试题目和整理的答案 - 腾讯(Tencent)
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 <class T>
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 T>
class Stack
{
public:
Stack(int = 10) ;
~Stack() { delete [] 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:从后面开始考虑,最后肯定要留1个才能保证自己赢
2:所以要设法让对方留下2,3,5个
3:也就是要自己取后留下1,4,6,7,8,9
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
7:所以很显然,我只能抽剩1,4,7
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
方先抽,也即是先抽的人输
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
--
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 <class T>
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 T>
class Stack
{
public:
Stack(int = 10) ;
~Stack() { delete [] 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:从后面开始考虑,最后肯定要留1个才能保证自己赢
2:所以要设法让对方留下2,3,5个
3:也就是要自己取后留下1,4,6,7,8,9
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
7:所以很显然,我只能抽剩1,4,7
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
方先抽,也即是先抽的人输
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
--
- 一些笔试题目和整理的答案 - 腾讯(Tencent)
- 一些笔试题目和答案
- 整理的一些PYTHON笔试面试题目
- 绝对有用腾讯笔试题目及答案
- 腾讯,迅雷,完美等公司的一些笔试题整理
- 腾讯的递归笔试题目
- 收集的腾讯笔试题目
- tencent腾讯——笔试题2
- 腾讯PHP实习生笔试题目及答案解析
- 【面试专题】腾讯PHP实习生笔试题目及答案解析
- 腾讯PHP实习生笔试题目及答案解析
- 整理的腾讯笔试题
- 笔试的一些题目 .
- 笔试的一些题目
- 笔试的一些题目
- 腾讯笔试题答案
- 常见C++笔试题目整理(含答案)10
- 常见C/C++笔试题目整理(含答案)1
- atoi函数实现
- 给定一个字符串,包含中文字符和英文字符,取给定大小字节的子串。
- 08-19 私有继承
- split函数 字符串反转
- 面向对象
- 一些笔试题目和整理的答案 - 腾讯(Tencent)
- javascript里时间转换
- redhat面试题目
- 2012/7/9Intel面试题目
- 不借助第三方变量,将x,y两个变量的值互换。
- 同学PB经历的面试题
- c++二维数组初始化简单版本
- 2.54转2.0小板改造
- 创建二叉树