|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)& h! X& \6 e, `8 u3 s9 R
4 w+ X7 G9 p- D! f
! s8 Q, [- M; f Y, p" [NO1
: v( K0 Y$ E0 ~Below is usual way we find one element in an array
+ |1 q' l1 Z% d g3 uconst int *find1(const int* array, int n, int x)% Q2 M7 \ S6 S' O. {
{
4 v/ A! y% N0 y4 v2 y, m- X const int* p = array;
6 o) D' g; j! W( ? for(int i = 0; i < n; i++)
$ T9 G$ T$ Z8 z2 ]: U3 }" w2 [- N- t {5 T4 Z, ~& g1 a7 d9 l% o
if(*p == x)
' b3 k' Z! W0 K. W0 z; `( p& y9 M {
& h# t0 w$ P8 [0 h' U! a return p;
4 S. Q! B; u( u$ S T6 u }9 \2 A; x: t! d- Q. j5 F. ?) T
++p;4 \" Q" \" w; y
}2 }$ u, Y' T# ] n7 P
return 0; }
4 k6 J" j. `$ l3 ZIn 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?
3 i$ K* @7 z' w& A, i. S$ W# D6 c6 |+ g, b% N
template <class T>
5 O, g1 r, K; O6 pconst T *find1(const T* array, int n, T x)) Q2 D: o7 X% j1 k
{
, Q+ c7 q/ m9 H% ?5 R/ e ^ const T* p = array;
3 z k$ c! p' o, H% V: Q for(int i = 0; i < n; i++)# L6 i! H+ {' W: ~, W
{
! x$ U* {' k& f( ~ if(*p == x)/ i6 q+ K+ U0 b' H/ w
{
% e; g/ M8 M( P0 \+ L4 m5 @ return p;4 V- q7 c, `5 y5 |% }1 ?: L
} Q u% @9 Q2 Q
++p;
2 z! U* v/ R0 r- _2 Y8 a }
9 V5 y) o4 l0 I6 {1 I$ r9 q( H# j return 0; }4 G2 l: S9 l/ _* ^4 M. F4 l
( A% T- a; a- SNO2
/ I, o0 C F* v. B% ~0 J: F/ O# E- f) A" P T( @8 n7 {: m
Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
( _% m: w/ J# j7 m: K$ P# ztemplate <class T> z3 Q! J7 J. @; g" b+ G
class Stack
+ }' p$ E. h! Y+ b7 z{* g8 w5 F! o, a( G& D% Q3 I
public:
$ m8 Z: G: I9 j1 L% e Stack(int = 10) ; * D9 S" E/ l N7 v8 y$ O
~Stack() { delete [] stackPtr ; }$ j/ L1 z- ?, l
int push(const T&); - y% k- u$ j/ A" `6 m
int pop(T&) ; . t$ ]- {/ ?$ d# v B
int isEmpty()const { return top == -1 ; }
9 ], i0 E8 M& L: P4 Y int isFull() const { return top == size - 1 ; } : V% m% E9 |( h) e! ^
private:% v7 H0 P- b4 m3 ?
int size ; // number of elements on Stack.' m$ n t& n& O5 S0 e
int top ; + e- m$ G0 A0 X
T* stackPtr ;
; Y' f' D& f# Q3 [6 Y) |- [6 P4 ~} ;
L9 H/ S5 r9 d9 M8 ^7 w: \* J. ] N1 f
0 F! N+ ]/ `# E: H' P6 M- kNO3$ `5 O+ G5 }0 S/ z7 b- j7 X
. G7 i3 y" I3 L; G: {% e
Implement the simplest singleton pattern(initialize if necessary).6 \1 Q- D' s6 l4 i
class Singleton {$ P+ |: J! a) O* T7 Y# V( B0 O
public: 0 L9 x4 b' x& @; k" D# ?1 g) R
static Singleton* Instance();7 ^, k; j1 U7 q% }! l, V4 v3 t
protected: C3 [- I2 k. e4 T7 K; P- `& }$ b
Singleton();+ j! ^3 [ A& T. \% ?6 c1 B
private:* T; }5 Y3 M) s! C
static Singleton* _instance;3 O# Z' }4 E4 S2 j) B/ s# w
}
. M# x4 x7 v' g: T( Y+ U q
% o' z, K9 ?3 I' a; U* V, l// Implementation " C7 `) ]' G4 n* C
Singleton* Singleton::_instance = 0;
: O/ P4 K0 m, z0 s, v3 Z3 H ~. I; H* I" {- Y& X# }
Singleton* Singleton::Instance() {: q, p% U9 T5 R9 f8 ^' V5 S9 s" H
if (_instance == 0) {
, A- a# u H1 o! \* n _instance = new Singleton;
( T3 e$ N9 v, B7 J3 D1 u, W }
/ Q7 @; U! P1 d( ~7 r4 ]9 F return _instance;2 [) b7 N& c7 K; ^( E
}6 g- I' v6 ^' z6 h6 e: s4 k* R3 r3 B
! C$ P; N6 @* n0 _3 z2 }7 d- {% ~$ s- g' @* @4 s. ? J
) B+ ]3 L e' g
NO4
) }5 y2 v% p; X7 r) P" w! }" S
' i& g3 M' c6 z% g6 c" U( D( I1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
# a$ p( L! F+ [, c4 @1)Totally 10 coins
3 k( A2 T+ Y! J4 M* N! z2)One can take away 1,2or 4 coins at one time by turns
" J0 L( d4 p" W& K! w& f3)Who takes the last loses.- y: B; e, B9 L0 A/ B8 t# t% q$ b
Given these rules Whether the winning status is pre-determined or not3 z7 U$ C, V! `# o( u
; E, m- I6 ^+ m5 t3 m! k: J
: B2 K1 S6 {: a+ F ~/ \' {" i
1:从后面开始考虑,最后肯定要留1个才能保证自己赢) f- U0 s- o, O2 z; A0 X3 h
2:所以要设法让对方留下2,3,5个
2 P5 L/ R e m l. S" U: Z3:也就是要自己取后留下1,4,6,7,8,9% J4 J6 U) J8 g i
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除65 k! G/ K6 w; x9 ?" u+ m
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
2 w k# i. L. e; I' F6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
7 f( X. _! m; `% H2 Q9 w- h7:所以很显然,我只能抽剩1,4,7
1 S9 A6 ^- G H4 Q5 \ F8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对; J }# N+ t. Z( A
方先抽,也即是先抽的人输
' x& x, \5 a5 Z' D% k5 v9 b( u; k) V( P9 V( q5 O
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html3 K; k0 A# j' P* P; |. T
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|