|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)6 ^# U: A4 F$ [1 Z. o! t
: ]& `3 q# }, R9 L
# x# t1 W% T: a4 ^9 P$ `NO1: x- \$ T. j# }
Below is usual way we find one element in an array
6 ]; p+ m# O' Y$ r( |* @const int *find1(const int* array, int n, int x)( \# T7 k0 F! x
{
: t0 ~) n7 M8 D! K9 }6 H const int* p = array;
5 o% d: \) {7 r1 {3 W1 \ for(int i = 0; i < n; i++)3 D3 s& j% Z- }3 _" i3 `
{
2 p+ q7 a& A. d if(*p == x)% U) d, r% Q! G; x' l
{6 P8 |, a: y/ d( k& F
return p;
5 H9 [6 p0 t- M: f }/ J( n* A& i- y L, J v
++p;6 [6 }+ n3 R" }( b
}
9 M& _9 Y5 A( q4 _$ w" D return 0; }
8 r1 w1 z: y* R; I) k) g+ pIn 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?
4 g3 A- N( ] Q$ g# w. @0 ~% q
, [* }- q% C! N( Btemplate <class T>! ^$ j2 I6 `' a" a. D: h$ C
const T *find1(const T* array, int n, T x)
1 z; X! G7 A8 a; L% p9 W{4 c# i& m; o0 s
const T* p = array;1 G. Z( Q* `$ r0 ]1 U
for(int i = 0; i < n; i++) h. E! d8 |" ~8 O
{
2 \2 A( t/ T9 U! W h% M# ~ if(*p == x)2 u6 d7 j5 Y- |4 a) q$ D l
{/ c/ k# l$ v7 a2 e/ w8 w4 k! F
return p;& l2 A' k' ~" @+ E# @* v3 a4 V
}/ ]: Y: D, s& w! Y6 P
++p;! ?3 Q) V* @5 f/ ~0 T ^7 e* K
}
p/ `! {- x& f! Y1 F return 0; }! M/ c1 e5 \( `
# e, G9 v* E8 p6 C
NO2
, ~, L1 N- z- W0 S6 r( \9 X
C! _4 f8 E$ J- M2 U8 KGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)5 V# v1 Q, Y+ n5 z5 r$ S
template <class T>2 @& m. S# M% |8 _' U
class Stack% O& m% C; ~& b) m, h8 x
{5 w B9 u( X) @) X' R
public:' k& o, t# Z4 Q( I Q
Stack(int = 10) ; : H( y- L7 _* R* J& J
~Stack() { delete [] stackPtr ; }" K; Y; e: N: A9 [$ |2 ^
int push(const T&); 8 s& k: f+ m! ~* ?& _4 n% x
int pop(T&) ; + m8 T* x# X; f \3 P5 S8 H
int isEmpty()const { return top == -1 ; } " m8 D& P: Y& c3 q7 z
int isFull() const { return top == size - 1 ; }
, u/ N- v6 d. h S+ N) pprivate:& J- Z- }$ A' G' V8 F
int size ; // number of elements on Stack.# R8 W5 I* k; d; r
int top ; - r9 g' \+ w' A3 w5 J: R
T* stackPtr ; 6 _* h- Y1 g# p/ z1 z
} ;1 B* P" S% v0 @) `: j
: X+ N/ m! K' o" x3 f5 G6 j
# f* m; k' N$ G% r: z$ z
NO31 W; D$ [& n L2 F k
4 u) _% f* k& Z- BImplement the simplest singleton pattern(initialize if necessary).
7 G3 \ @# \2 cclass Singleton {
1 i i" `% O3 E0 Z" `- h8 q5 }public:
& F- t, e, ?- i/ f static Singleton* Instance();; Y. M9 i7 u1 p }. e/ k
protected:
/ o- @, I; d6 x$ X1 k7 t% ^ Singleton();4 S# A+ @7 e7 j) q3 ~
private:
4 h t$ P4 G# h$ s! O8 A; g& M static Singleton* _instance;
: E0 A. x( Y2 B7 t4 S, s' T}
: @" d4 Q0 M' d+ Z m
# V; [5 S; \* m( a$ u// Implementation
- `! M4 t* t9 K2 `Singleton* Singleton::_instance = 0;. k5 ~: ]2 B2 y/ \$ u! R
9 K; K* R- G2 s' X: YSingleton* Singleton::Instance() {1 U( {5 v" Y* g0 j! j
if (_instance == 0) {
$ C L8 q* m' H2 h8 l. u; q4 I _instance = new Singleton;* K+ v& A5 Z' N
}4 E5 Z% ]5 H, Z% H
return _instance;0 x3 Y! D& D/ H6 B. G$ b* q" `+ n
}
/ c( f1 Q/ T( ?1 Y* Q& r8 Q7 F/ `, n W1 J) b, T" N( \
) L, ?2 i- y& J& p0 s5 i- b) x+ u# ^
NO4
- z! p7 e X7 L
; `: j+ l1 ~1 z6 Q5 R0 U1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:5 r8 r0 n' v- @8 V" p
1)Totally 10 coins
6 [- T' J Y, t) `2)One can take away 1,2or 4 coins at one time by turns( A0 }' D. F2 p' Y6 b) m' I) [$ k5 f
3)Who takes the last loses.3 A0 ~ I1 X- C3 L) y' M; U/ ]+ ?
Given these rules Whether the winning status is pre-determined or not* Y2 V! Z% n1 n; R6 ~+ A* y7 r
/ G5 u$ v s: j% |) \9 B2 X; N: k' P
' H4 T, j9 T! }9 P1 G: |; [2 g2 n1:从后面开始考虑,最后肯定要留1个才能保证自己赢 i( K" c( {' E
2:所以要设法让对方留下2,3,5个% R' {3 e, n& j1 n# {3 Q
3:也就是要自己取后留下1,4,6,7,8,9. }* o1 Q9 W$ \/ c! z5 [
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
) u+ J6 k1 r% H3 Y1 c+ |" |+ {5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
$ R5 A7 @- G& `1 q( M" J& z4 r9 Z! ]6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
1 C: [; t7 N% w8 o) f/ C1 f7:所以很显然,我只能抽剩1,4,7
" G4 K+ g$ Q) l9 w, T: @% X& [8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
5 p; t: U r, ]4 i9 @6 \1 W8 r方先抽,也即是先抽的人输
3 y( t6 @5 x5 c, C" j( g9 u4 s* G- k5 g6 H, H# c: x
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
6 S% J$ C# W' ?3 k& _* P8 g. j2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|