|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)* w, s" v$ y( t/ W
$ k" L4 S; }$ Z/ j
8 O4 t8 g4 l; w) Y# K5 iNO1
' {* m" r& {: E# e% A" f; F& h7 F" SBelow is usual way we find one element in an array
2 }$ P" j0 @# }; J: c0 U; a# k9 {# Econst int *find1(const int* array, int n, int x)
6 f9 d1 A& r" `# h4 v5 H9 N{
% {5 |7 K/ H& J1 D8 D$ b! ?4 `4 r const int* p = array;
" ^, V- ?8 N+ b: E2 q6 E3 s for(int i = 0; i < n; i++)8 e5 U5 [- S! ] u/ c( X5 [
{
9 H; y/ R$ M' y! E U if(*p == x)
- m3 M5 s# v" ~, `" W; L {" Z3 h% ~$ T% f; R: o
return p;
) k2 g7 X2 m, w! q1 S. Q* @0 U6 \ }
: s ~" a; L5 r6 V/ ?3 } ++p;4 j- G. f! k7 h: s. E' r
}
8 M P. _+ c w. \. ^; b return 0; }
9 W3 h) M8 U! \7 @* a! i. HIn 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?0 \: c% ^* _. y3 V0 A
& D8 t, |* M9 C0 }/ Ztemplate <class T>
! Y \6 I) _7 oconst T *find1(const T* array, int n, T x)3 P3 U; [1 o2 p1 e$ j
{$ I: F) w* f) F4 g3 R( }6 {0 {' `
const T* p = array;
% l: S( V3 N6 d% ^ f* M for(int i = 0; i < n; i++)
8 ]6 m1 N4 P" Z% @, b1 Z' d/ A+ Q {& P/ K, ~7 | u
if(*p == x)
6 w* C. O/ ^& O X, z, ? {" }/ @. o7 D$ a+ p' Q- A7 {2 E
return p;
, Q0 S1 n/ j9 v8 o6 V) e }
6 {5 ?' m( m$ t4 h ++p;" b: ~! d; m8 E: S, ]# u
}
2 E4 B# ]4 o, X/ R return 0; }
. R0 ]) l1 }$ e( L- y5 ^$ {) P. S( ]$ E- C( q+ F
NO2- t! M& R6 N7 `' s ]( c) L/ T
1 x0 I& k. d& i u- e# x( s# D" [Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)3 Y0 k) W! Y# r
template <class T>
& o+ k" R/ o, Q9 tclass Stack
+ `7 c8 v4 n* Z% w" {2 a{0 G8 ]3 X; [" T+ L
public:
1 |2 J% M3 S" b' y( x Stack(int = 10) ;
7 S, Q/ l) @ a( r ~Stack() { delete [] stackPtr ; }9 H* w' R* y, Y/ y6 b& z
int push(const T&); * i+ X) l, n# W2 i
int pop(T&) ;
& p G# Q6 ^! B A int isEmpty()const { return top == -1 ; } - w! O$ ^/ x T. g! R/ f
int isFull() const { return top == size - 1 ; }
1 Z0 B/ a4 A; \2 y' J5 B& X4 \/ Aprivate:
/ q+ m( b" r2 Q9 r int size ; // number of elements on Stack.1 R0 ^& Z0 Z& D* l4 n$ B/ P
int top ;
' G7 a* h1 T/ E7 V" N T* stackPtr ; / t6 u ]' f% W5 M3 a4 u( o
} ;
# ?9 c# ?4 d2 s2 L" I- _. b7 l& |0 m3 c; W( ]
$ b: E6 j2 T( hNO3
, v0 w9 H7 s5 U! `8 f9 J, u" p& U1 X
Implement the simplest singleton pattern(initialize if necessary).
/ M/ U7 Z4 V0 g4 V0 Wclass Singleton {
# T1 v! C$ |" gpublic:
: m5 }9 U' ]8 o static Singleton* Instance();+ {, y- m$ E5 E
protected:
. ^$ f V, M9 [( j5 n# ]3 H4 C" M% o Singleton();
# o& E ^* G( |private:
% Q ]( O: V7 Q static Singleton* _instance;9 N) ?( t4 ^$ a" U8 z
}
* S/ h/ n+ [: }' e
$ R! @; F* ]9 F' d// Implementation 1 k- i6 I4 l* S D
Singleton* Singleton::_instance = 0;. h2 I0 c. C1 r5 ^8 N) d' U8 |0 b- Y
1 `" x3 J* e5 s4 ^; X; X* dSingleton* Singleton::Instance() {4 `9 I- W, Z# Z& z
if (_instance == 0) {
- j9 N B, Y0 z6 p3 ~ _instance = new Singleton;
/ s4 _9 ~) ^6 S C, x' W }0 a# x" ^9 z. @
return _instance;
% Z# q4 w7 E( N+ z: S* I( k}/ C% u1 C# F0 i, e- p) p, y
* Z: n/ H7 [, g; x+ L$ K' Q
# t% {- h' D- e5 [; I* Y
: r5 W5 ?1 r- b4 U" [; |NO4$ O+ v/ D; _+ ~. W/ x: \" z
+ X& ~6 Q1 D2 |# a9 S. J# v1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
* D0 Z& w6 ^* a1 N- n+ K2 @1)Totally 10 coins+ |- h. _2 Q$ \8 D
2)One can take away 1,2or 4 coins at one time by turns+ y5 `/ ]. t* V8 O( ]
3)Who takes the last loses.& F5 x% C* M6 Y# c# T7 ?+ ~% J
Given these rules Whether the winning status is pre-determined or not& w$ m8 d W4 n1 r
4 I/ Q" \( K; c* ]9 Y
( v+ F$ L- a4 a. B
1:从后面开始考虑,最后肯定要留1个才能保证自己赢
. H* n% Q* `9 h0 n" [0 ^2:所以要设法让对方留下2,3,5个. W! B4 I/ g" e
3:也就是要自己取后留下1,4,6,7,8,9
3 ^: q! |) K" X8 F2 C' Y4 }% S4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
5 f& P* _- ~* s3 D4 q0 u5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
4 ?# G" _: \/ `) o' N9 j. e; A G6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
+ g, \/ w$ F& _$ X+ q5 P7:所以很显然,我只能抽剩1,4,7
5 R) R4 ]" [) Z" O% M' t8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对& A% x; W$ p$ e
方先抽,也即是先抽的人输4 C4 L4 z" n5 M9 {: ^5 Z7 L3 |9 c
: l( i) O! p1 `3 j腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
& x& X. n4 _* {; v9 E0 c0 j2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|