|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)
1 ?( u9 M3 m5 |, h4 V
" X6 S% v: _( C( Z. ?
5 R- P# r# z- Z! Y" u" [8 o* O5 HNO1
/ _. d6 N/ O: `9 Z( d, V+ PBelow is usual way we find one element in an array
0 q0 |0 X, G, `' z/ C% qconst int *find1(const int* array, int n, int x)
% h9 \- w6 F* R0 s2 b{$ u: u o# H" Z' ^
const int* p = array;; n. ^! C# ^8 u
for(int i = 0; i < n; i++)
2 j- Q$ ~' _# Y" r* Q% C {
2 C5 {3 h8 N7 ^4 S; O [- ? if(*p == x)& c/ {; i4 ^5 m$ A
{
$ _, B6 ?$ H5 O1 g; y$ I return p;* g( f. j7 N3 [( H8 Q
}
" N9 s# L2 n3 x- K3 I ++p;
% m: Q* W0 B R, L# Z }
/ }+ P3 w0 J( c2 w: ? return 0; } Z$ N# i6 p, h! _" Y
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?% M1 Q8 ~4 D7 Y- ?2 X+ ^9 c; D. \
, T! @( c7 l- u2 M1 o k* Ctemplate <class T> \0 L' e6 p9 k% ^/ M2 r2 w) b
const T *find1(const T* array, int n, T x)% Z$ m6 J% A5 G. g) _$ C0 \+ T! K
{ _0 V* R) h) Z# r' o6 B
const T* p = array;
4 G8 ?8 I$ t) f" o6 O& W: d2 U7 [9 i A& p for(int i = 0; i < n; i++)
3 G* r) L# j; B {* N( V+ ]4 |" v% l% D: P
if(*p == x)
* O7 C! W# n) X+ ^4 X8 s8 x* P) p6 z* j {
0 [$ t' } V& i1 x4 o" y5 G return p;
0 a9 V6 u( M7 `* x/ j0 s }/ W5 ~ h2 l' `7 p4 x% Q8 S
++p;
# s7 j) g z' b* k" p0 D }
5 e$ B0 k4 N8 S1 S- @+ t return 0; }
( |( s- x$ e; ~0 \ n2 d1 c u5 ?+ g/ o' I+ l' l' ^* j
NO22 h* M+ a1 I7 x+ P
n( Q) Q3 E4 Y1 HGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
9 Y4 _/ L$ V0 Ctemplate <class T>
) j" Q$ o7 \7 V0 L: ]7 ^4 Hclass Stack! Y: {6 z; e* d5 Q9 c/ W
{
9 u9 q2 x/ d- J, a% ]" p8 Wpublic:
2 C& o. Q5 _: @6 h. [2 l. ] Stack(int = 10) ;
& T4 ~! S- C" T# N ~Stack() { delete [] stackPtr ; }/ |# C8 t; @- e5 Z$ N! q
int push(const T&);
. e! v% V O; }0 G7 A( o int pop(T&) ;
( W9 V1 M5 A \7 b int isEmpty()const { return top == -1 ; }
' x$ I! d. M8 y G! n, k int isFull() const { return top == size - 1 ; } + @4 t" u7 b6 C5 c! p
private:( l H* W& K0 L( u' X+ f4 g
int size ; // number of elements on Stack., x/ t. S) d; P8 x3 c' Z0 Q
int top ; 7 O( G6 N# M: E5 Y! d. j% _! E5 l
T* stackPtr ; + c: q1 Q U8 P; `0 H
} ;7 o" S$ q* {$ X; z
" {+ ?& R, M1 T* T
4 S* @$ p% T. [6 v# sNO3
0 ^; G' h# o6 G3 m) G5 \6 A& ]5 [
! ]9 Z* z. |% m& _5 X8 }Implement the simplest singleton pattern(initialize if necessary).
# Z3 b3 o. E7 I- J6 C4 E) C! yclass Singleton {
! i% `7 f5 [0 P7 y# B. }/ T3 cpublic: 8 p! g% Y, M; ]) o- ?
static Singleton* Instance();
1 N1 i2 p' x' _; G0 T% Y0 {protected: - E; c* n9 F r
Singleton();- e( d" N- O% d- l7 v
private:. ]& b' U: R7 l% ]' x
static Singleton* _instance;( A5 R' J1 C3 f
}
4 c( ^7 U# @& `% X! j
, W7 w5 n7 w" n: x. T- _* O2 E// Implementation
8 a9 B3 B/ I) B6 `& k# ?Singleton* Singleton::_instance = 0;6 C5 a/ I: V+ M+ r
- h0 M y8 h7 z( D
Singleton* Singleton::Instance() {
- A2 g* S T9 A" R5 r0 @ if (_instance == 0) {
8 f" E* a8 ~) V/ h _instance = new Singleton;
7 [- D7 D% @. k }
& T7 r) c0 x5 W$ b* d V! \ return _instance;( |4 W6 p) ` S) T" H1 u& L
}
3 H3 Z! o% E3 B
8 C# j) C. V) O/ [# p# G! Z( R% g7 F6 o8 k1 f! W; b1 T+ f: o( H
4 ~/ g: _+ S$ t0 E# V9 f' o1 XNO49 q! a* O+ H# C1 v1 U7 j, g+ }
6 Z5 Z8 s' O' t4 R4 |
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:$ D5 u6 c/ U$ e; y( Y( G5 d
1)Totally 10 coins
& ^ `8 f" c# }1 m5 d1 G1 ^# ?2)One can take away 1,2or 4 coins at one time by turns# N. Y3 [7 l4 a
3)Who takes the last loses.
: ] Q. c+ l) P G- NGiven these rules Whether the winning status is pre-determined or not& D. R$ S6 |2 c" T( N. e; U2 D2 {
7 {+ E. d; ~. z1 H- j. i8 a
; u4 u$ s: k0 k* c- o1:从后面开始考虑,最后肯定要留1个才能保证自己赢5 O- @5 f% d C) j
2:所以要设法让对方留下2,3,5个
# F4 s% z& |6 O b3:也就是要自己取后留下1,4,6,7,8,9
- _: a2 L- ^3 s* ]/ }% _9 p* J9 e4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6# B q1 }& }/ n& i4 t
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
( n' y& W; `5 D+ t2 s& R' N6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
4 p. @1 ]3 R$ ~8 f2 ~$ u7:所以很显然,我只能抽剩1,4,7
+ `+ a- p% ]1 u+ U/ a+ x/ H8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对5 P9 P6 o0 R# l0 d' W
方先抽,也即是先抽的人输
: ^* H( B) a) ~4 x, l3 X# t; H3 S3 j) _; F
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
6 _( G- H; @7 G, X2 O2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|