|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)
, Z5 c7 e. ] q9 l- I% ?! B9 T. n+ r0 [1 o! Y. o
( J' T$ j* p4 p% t* I- V/ S0 w
NO1
! D) V+ d5 J" I4 Y0 d! o+ xBelow is usual way we find one element in an array
9 `+ J0 `3 [. b k) _const int *find1(const int* array, int n, int x): F' P9 D2 I7 |
{7 q3 n3 A$ L$ x- d4 v
const int* p = array;
- I S- B: n7 L2 [. k for(int i = 0; i < n; i++)' u! P f" l" m- w$ e/ _7 T
{
! M* U( a% d, L if(*p == x)
* r) N6 S8 z; {" l9 r6 { {4 m2 T; W( u& ]3 c, D" A
return p;- B1 ~) [/ U3 c5 Q
}5 d' Y m( G) P# H9 Y+ U
++p;
3 F( {) }, q: I2 X+ Q; V0 ^# z }
9 \' P+ j0 }) V/ Q6 V return 0; }" ?" b7 [2 z: F* t$ p! f, I
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?
* \6 ]) h% X& H/ J9 M
- p; W6 i: [* R2 ]. w1 C' _template <class T>
; ~. f6 ~- ^# M w) u. `7 G+ \ Wconst T *find1(const T* array, int n, T x)7 m. e0 a1 [, B4 e
{. p9 N& @' r, b- W( I8 c
const T* p = array;
9 b* w0 H- U m$ e for(int i = 0; i < n; i++)
. p2 j2 Y* C' o; r {: K. O+ w8 v- s+ T: ~/ E
if(*p == x)$ U# e' _9 X9 U% ]
{% s+ |% y9 O" t) m. X$ w7 s C7 F6 D
return p;+ k! ~9 B3 A! T& h4 J
}- X+ Q# }3 [% N3 ]7 N
++p;. J8 l: ^- g- h7 R% E* F7 E
}6 M% Q4 o7 J/ A- X! Z
return 0; }# W! U6 B' Y' X8 i- F
$ V- t" i y: {& q% wNO2
& k8 V, X8 ] e! d9 p* C' t' z M0 G! U8 J/ H) j" M- j
Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
# h& [1 Q2 l$ h% H. l8 [& Ytemplate <class T>5 v8 {3 h+ w& z
class Stack
! ^2 ?, }9 b$ o& _: ^- u{
$ ~& v: T; O9 T- L- `public:
5 w8 L3 k3 p+ Q- q6 o/ Q* `$ ` Stack(int = 10) ;
! h) d, f/ l$ O! _" s3 D ~Stack() { delete [] stackPtr ; }' A9 b2 V3 y7 @/ d
int push(const T&); $ n/ G; X6 H2 ^* J3 _
int pop(T&) ;
: r! K% n: h6 d+ v9 ] int isEmpty()const { return top == -1 ; }
* y# b$ G' C' | int isFull() const { return top == size - 1 ; }
2 @2 D5 O7 y7 D2 [1 `" yprivate:
/ Z6 t; @! l$ h int size ; // number of elements on Stack.% M3 e- X$ y( U9 K, M' o6 ]
int top ; ) b1 J$ b( Y9 W @
T* stackPtr ;
* [3 i9 Y- j3 ?* u1 n. M9 j4 p} ;: G7 c+ T6 B" E% P) @, u2 J8 \7 o
9 V/ u; i( d% f+ _5 h6 j! z' K
7 i u. W' R6 ?# m, GNO3
; E o0 N* U% R" P0 a1 ~* {1 z1 @+ G9 V/ m
Implement the simplest singleton pattern(initialize if necessary).
1 D4 \" o! J0 O, d+ {4 ?7 R; x9 ~0 Hclass Singleton {3 f! K- l$ a8 h( f2 ]% ~4 X
public: : U! D9 s( C2 A% e( s X+ H
static Singleton* Instance();
* N. j3 D# f1 ~$ a- ~1 L. K; ]& Kprotected: + b/ V6 R, f3 T( x
Singleton();- Y7 ]0 @) t! n9 w' G
private:: c9 ?1 t9 C+ L. K
static Singleton* _instance;
* |, }4 R5 \' S3 I# D}
: E- I8 `! }# E7 @2 P4 w8 c2 x# |/ I G; S/ R! ~ T) x5 v
// Implementation # L; D- `6 a+ X; c/ l' j
Singleton* Singleton::_instance = 0;" z; c6 t7 e. R: \. q z, k2 q6 d
% c# ~! k! w. z, x% o2 u
Singleton* Singleton::Instance() {
- j3 C+ S. f( q5 R: V/ L if (_instance == 0) {
# ^1 x! U8 M1 I9 X _instance = new Singleton;
; R. g. h2 W" n( W' B' `% j ? }7 H& U3 s/ Y- B( \& u
return _instance;
0 A+ E0 }1 |1 A6 N}6 B* l) z S. Z4 z& y" y3 E5 Y! ^
$ }0 p: o4 c4 H7 V- ?& l
+ B$ ]& }# C: n! E1 L
2 a! k; z& P6 z7 v% K9 ^- VNO4% a l1 ^ f" e' t9 v9 R
& i! i0 c! t* O. o3 v! l$ ?9 ^8 j9 N' p
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
9 }, r: s- d0 A. x2 D1)Totally 10 coins% o9 z# I2 g7 I8 v7 U0 n! u% f2 b
2)One can take away 1,2or 4 coins at one time by turns7 S; v4 w4 ^+ v/ e5 L
3)Who takes the last loses.
! h1 X7 t& P' `Given these rules Whether the winning status is pre-determined or not
$ U$ g+ d9 o7 Y" T: w: l( G* Z7 z* s/ w
* y/ ~5 j! I8 D0 @! S* n6 S( T1 j. f% g' H$ u
1:从后面开始考虑,最后肯定要留1个才能保证自己赢
6 f ] E, P) _$ i1 E2:所以要设法让对方留下2,3,5个
5 S$ o$ m O" O2 p3:也就是要自己取后留下1,4,6,7,8,9
) k1 k# P1 ^" o7 }9 g4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6+ t, Z1 F& ?+ m N0 ]: z0 R s
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
! p) _9 F9 b% l+ ]3 K: v# N4 @6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除9 _5 n; O# s7 j# ]1 ^4 n, F
7:所以很显然,我只能抽剩1,4,75 e X* B0 [* V& k/ S6 Z
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
5 V/ }5 c: P9 ?, W6 z6 b方先抽,也即是先抽的人输 T3 h8 ?3 u \. I4 f4 W
3 p& y% C+ Q g/ \+ f E f腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html3 f3 W1 Z0 e' N; `) p
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|