|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)
* t$ c* u; u1 o' T- a t. n0 D2 W5 U) r
3 P( r, I2 |6 z2 |- Z* _
NO1
O, K* L; q8 R, M6 W) D" oBelow is usual way we find one element in an array
' b8 C9 q* [2 g3 X( C2 L6 @* gconst int *find1(const int* array, int n, int x)" J0 S# p# [ @% g
{1 m" f& q( M7 p, u9 \% o3 e, l
const int* p = array;
6 `! W* j: V( g for(int i = 0; i < n; i++)% F: b, b& y' M) m5 @2 J
{* q. A4 @ G7 p% R
if(*p == x); U1 V0 a9 H, C8 j! f* s
{
0 ~' f0 F( u! G7 `8 v return p;6 i) [2 O. r* T" m- Z
}7 b" a" }; }# z5 }( n+ @. J7 G
++p;
1 L( g9 w( v! N( f& K+ o2 { }
& C1 q2 k7 Y+ e$ B4 v s/ V9 K return 0; }
( A: V/ @( `* N) D6 r7 w2 vIn 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?' _$ ?* M1 b' O/ w
! L2 V! P6 \4 C* H0 X( S% Dtemplate <class T>
0 i6 ]' V" |* y) M1 I! \: sconst T *find1(const T* array, int n, T x)
4 K5 E. G6 `" }' l9 h+ z{( q- q7 J1 `2 d" `3 f; Q
const T* p = array;2 O' z5 U# ?$ p! o* } A; B
for(int i = 0; i < n; i++)
5 ]/ ~% O$ m$ x. \1 L+ h! P) ` {
& p: Y* ` Z K, j if(*p == x)
* y @ U2 D- Z( o/ I+ g {
/ v2 @" j; D! |9 g8 |7 x return p;$ _4 ]. S, E8 a9 E6 U2 N
}# w& t6 A: t$ s' w) _- s1 [
++p;7 i% F* C5 G8 g6 g, _% B/ o
}0 i# f) B5 P2 }2 }! W
return 0; }2 U3 ~5 y0 J( j: N' Z8 @
7 i- L- J4 \+ }! b! fNO2
, ?& w" {+ l) k" n k0 R$ S- X
- ~2 w( t6 I1 l! _2 I+ R D+ IGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
; V. D! f: v0 T' B; ]template <class T>
m5 f9 D9 L* P) L7 @7 pclass Stack0 E) u% k* S2 l, X5 }
{! ]# g# v/ q# z: P( v9 T/ z9 C4 i
public:( } A: s4 r2 ], E' B+ J
Stack(int = 10) ;
6 `' L* F3 x0 s9 B0 j& k& V8 F ~Stack() { delete [] stackPtr ; }
) c- a& a$ F& ]2 ?* ]' r5 Y int push(const T&); * o% q& y7 Q9 K( D
int pop(T&) ; 0 b% [7 G k! c9 }
int isEmpty()const { return top == -1 ; }
* U4 s: a" n1 @5 T8 L0 W* |& O/ l8 X7 r int isFull() const { return top == size - 1 ; }
* d5 m" C( S8 j6 {+ a9 \private:" @) {! v: h5 }* t, u. s9 I8 _
int size ; // number of elements on Stack.
3 q, ]" p4 Q6 b0 C& O int top ;
/ v0 E" R- |' [) X& j' u T* stackPtr ; 9 `4 s, H' g7 E1 t- K7 V
} ;& k' r: G4 c( A
; m3 b2 A/ E; }0 f6 F
7 B8 J. U& p# X! \- |* Z
NO3
- Y) @3 c: o" Y# z! i; B
& a Q1 b1 h+ a* CImplement the simplest singleton pattern(initialize if necessary).' B" T) ?+ J( @$ k! H: |
class Singleton {
( k4 ]/ B) a( ppublic: " \9 P: y9 V" [1 D, w
static Singleton* Instance();, r$ l6 o6 R& h/ D& k
protected:
) n. D& S# ]9 W Singleton();
$ N- U. s4 \2 p$ K7 nprivate:3 T u5 m" C8 e
static Singleton* _instance;& d8 r7 m% |! z
}
3 o3 d2 P6 R2 J3 N5 y4 A# J% `8 @6 j, j0 Q, h* ~& x! D0 i
// Implementation # u* K- W Z& {5 @1 N
Singleton* Singleton::_instance = 0;, w$ l, s; D/ S6 H6 E0 N) z
* |8 A: x* \' E- ^Singleton* Singleton::Instance() {* B- ]6 u% v( N
if (_instance == 0) {' h; D4 w* ^, ]6 Y
_instance = new Singleton;0 s7 h5 Q) W! A, N
}: a' D. ~; w. p% K
return _instance;+ W3 e9 ^/ `1 K
}2 _* T! q2 r6 Q6 v
& C* ^. n% _/ n7 n" c" [0 ^/ [
/ }. N: X' `3 J, t- N: j
. D6 x( S$ z. a* W; {NO4; b6 |, V+ Z/ j% ? e3 `
2 O9 r* U8 l5 B% \6 l0 C
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:9 B% a/ r {/ g
1)Totally 10 coins
1 B+ q& N3 |7 [0 y2 H2)One can take away 1,2or 4 coins at one time by turns
+ G5 U& _, u0 K( \- [3)Who takes the last loses.
" s- I2 `) ?$ P' W7 p3 S* z, SGiven these rules Whether the winning status is pre-determined or not
( B# N: S1 X4 {' z+ a) g9 u- V2 r+ u$ ]
) g% S1 p5 e5 }# z! P6 w! M1:从后面开始考虑,最后肯定要留1个才能保证自己赢
+ `) Q. c" w6 F2 i' w" y* T2:所以要设法让对方留下2,3,5个/ k) A9 I+ a( x3 ^2 }! G
3:也就是要自己取后留下1,4,6,7,8,9
4 C6 _* W$ i: s$ D/ g [: d6 ?4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6- A6 A K1 ]- t9 S3 g, P5 A- A1 s7 |
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除88 Q, C( y M- y8 l0 T
6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除) j P, K! p: U% I* u1 u
7:所以很显然,我只能抽剩1,4,7
) B$ c# D* u0 ?5 C# L& r8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对5 k% [/ w: I! s+ S$ j& H
方先抽,也即是先抽的人输5 O/ W t+ }, C: z8 x9 d
# n8 E4 a; N9 G- l6 M& A/ P7 H! B% n
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html! \( \: D H6 V/ \5 A
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|