|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)
! e G9 l# V% H% R: }5 t, E% {2 M$ y/ z w1 b' t5 a1 o
+ e1 V7 Q' R& I( ?) `& i5 F mNO1
. @9 [* r7 d* T, ~) VBelow is usual way we find one element in an array; p" J& W$ }0 ]% g4 p# _! R
const int *find1(const int* array, int n, int x)8 e: h H6 `5 q9 k6 { ^
{9 v/ H# H5 ^) v1 P
const int* p = array;
5 h. V( c% ]5 X/ N5 C for(int i = 0; i < n; i++)* \! y( n3 L- x D! Z
{4 I% K3 R( [ Q2 F6 L. T6 t
if(*p == x)+ n. e# U* |; H( f2 b
{
) k7 K- W' y1 U8 | return p;! E* t( r1 ~ H7 S
}
5 L3 V7 Q$ C6 { ++p;
' Z+ x$ m7 c8 ?' T, S5 \9 G }* F' w+ [5 x: j: _" T' e9 C
return 0; }) S; p5 _4 \$ A6 t
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?: S- j3 n3 c; C8 G" ?# E
# N% y g: g# M" s& A; z9 }# K
template <class T>$ h% h& z9 n5 R9 i* Q, V% D1 b
const T *find1(const T* array, int n, T x)
$ Y: @3 C- \. W3 |{5 C1 }) L L. b* `9 T/ V6 j
const T* p = array;
, ~7 ~& w$ w2 I- o. S5 ^ for(int i = 0; i < n; i++)
7 N- d, J" \& r2 ]! m$ f {: T. N" U" E( r7 q- y
if(*p == x)
7 ?5 B8 C8 |$ R! l( f- _; ` {* ]$ J: G P" J6 b# Y( |
return p;
# H1 V, t! {( U! I4 C* L. x }, f3 o3 F& m6 g
++p;* n7 t9 M* @0 R$ }1 o* u- r$ d
}; i7 M) T+ h6 j6 q2 i
return 0; }( c6 M; A! K8 m, `2 a2 p# `0 d, {
/ D; e0 ` Q: ^" k4 R# `% C
NO2
) l* G4 \$ @8 i7 h2 v
- ~! b* e! b, F2 b: k, B1 oGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)
/ U. T' Y+ W1 I8 V9 `$ F( B* vtemplate <class T>0 g3 {- [, W* s/ F! {" |: Q7 p. z
class Stack1 w( I9 t, [4 H* R) N
{
7 f0 t5 B3 }0 tpublic:$ G* _ b0 ~" F$ M' e* V, r
Stack(int = 10) ;
" d% X! f& C2 g ~Stack() { delete [] stackPtr ; }$ S( ^. Y# k& G7 f- _
int push(const T&);
# }. U1 h2 ^ y6 U; Q int pop(T&) ;
# p( ]9 L8 p* {4 J' f1 k# B3 h, \ int isEmpty()const { return top == -1 ; } 5 B. r' |$ ?. \, x3 X6 D+ t9 G
int isFull() const { return top == size - 1 ; } , l! R1 e ~' m3 ?4 g; g
private:
8 u+ A+ g- O" t' ? int size ; // number of elements on Stack.9 D( ?0 M: @% U/ l# E5 [( Y! M
int top ; 8 B; ~! u3 \5 f- C6 P
T* stackPtr ; 5 B7 E9 `7 D# l8 P) K j
} ;
7 s4 K+ e! Y: C n5 i0 D' u9 @+ w9 z, t5 l; a. m8 [/ w
/ [3 n7 X9 E5 K- G3 ^NO31 f& d r# Y" c
) m. m: P( @7 c0 x- l5 X4 @
Implement the simplest singleton pattern(initialize if necessary).
) L* I4 _: U! |1 R7 m! L! I& c* J3 Jclass Singleton {
0 Y; W: v9 H5 l1 n; }public: $ G. Z, N. ?. N6 Z5 }; v# Z2 V
static Singleton* Instance();
D0 S$ U. ]/ @6 T/ v, }protected: 9 A1 F5 Y$ J+ b0 }9 g) G: e
Singleton();2 j$ d6 B( C: \3 D+ r( ?
private:; p5 ?3 i5 F; @0 h4 _. v+ }
static Singleton* _instance;
0 J5 @9 H- u8 Y) @6 N* P! Z: O: W}
* t. @* V7 F I% r+ L
2 e; O) I4 n6 Y2 `, w$ W" y; K/ d// Implementation
6 |& ]9 C% }- ]Singleton* Singleton::_instance = 0;7 P t4 d2 X4 X
- l7 X5 k0 L @/ R- x. r+ N
Singleton* Singleton::Instance() {
1 N2 k2 u- h4 I# f0 } if (_instance == 0) {( ]' `9 v/ i& V* ^0 Z
_instance = new Singleton;( ~/ X' w% ^( L% k' p* ?
}
; A: B( v2 x3 Q G c- I return _instance;" \- v, e0 q* X- O7 u: u+ ?( D! D
}7 C( I! K; b# O
$ e! \; l% D4 ?: ^+ K5 N
" z& x2 ~+ s: d% D( \9 L( |, O/ I& P2 V* v
NO4) n5 J+ d5 x. V) x5 X& s
0 @& N4 ?% r) Q" Q! o( T( l+ |1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
2 T7 l! g; s* \& t1)Totally 10 coins
9 M1 A2 u" O5 C# G8 m2)One can take away 1,2or 4 coins at one time by turns/ X: N% l$ x, c2 r. ]3 t6 a
3)Who takes the last loses.
4 ` ^7 i1 R( F6 bGiven these rules Whether the winning status is pre-determined or not
1 Y& h0 c% m) X# U6 A
2 u1 a: R3 |( W- o, n# Y2 ]
1 r! L' S# r% J5 G! W& f* r1:从后面开始考虑,最后肯定要留1个才能保证自己赢1 z+ o( w# ^1 ?& r! l, G* c
2:所以要设法让对方留下2,3,5个" H8 I6 O; `, b3 C
3:也就是要自己取后留下1,4,6,7,8,9
* P8 l; `& Y7 J( L, @4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
( W( i1 a2 p$ ?2 k; u5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除81 R& T9 H) Y0 O" ~ Z$ @
6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
$ i. k- K" H+ H e7:所以很显然,我只能抽剩1,4,78 D2 n1 y* U' X) n1 k2 k
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
; K+ g4 Z: z$ s方先抽,也即是先抽的人输
3 q, J% X) }3 }6 v, e7 Q" |
+ q3 W& K: p3 t. u% k' M腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
/ l$ i4 N: b* B2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|