|
|
一些笔试题目和整理的答案 - 腾讯(Tencent)
" R+ P+ {9 o4 K# h6 I- p
6 ~& G# H/ Q/ c e: v' {
$ m: O3 ^) o7 e s* [# qNO1
$ r5 U+ @& ^+ s* |. L5 V3 RBelow is usual way we find one element in an array
/ E6 |( p* ?8 u- E( v( R8 tconst int *find1(const int* array, int n, int x)
9 |7 p5 u3 j) u! g{$ c4 ?7 [' F. c+ u+ I3 |
const int* p = array;8 I3 U. @& }" G% C9 V3 t
for(int i = 0; i < n; i++)
9 d* Q: c/ ^" P3 ]& s {
7 P9 R7 ^* g! F2 p if(*p == x)
3 ^( v: V# P _6 Y {% B. R( C' z) ? q' C5 z/ X
return p;
5 j2 m/ v( s8 A( K. ]& z: A' I }9 c! Z+ H( S8 N; u- ?6 y
++p;6 k& U; D6 Z3 D9 c; Y9 n
}8 I+ A# E3 Z: C8 C" D
return 0; }
8 b3 |. f8 G# E9 }( Q, 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?) s- M0 F: U8 d
T" m5 [* M0 @2 Ftemplate <class T>) Z3 h- \7 V" @
const T *find1(const T* array, int n, T x)
8 y# A1 s6 u& C) C' p% B{
7 K! l0 y+ L8 S const T* p = array;9 `: ]5 z# }9 p# `5 V
for(int i = 0; i < n; i++)
6 b! T4 ]. n3 {% L1 L5 b1 W {
. z2 h9 s& U: l+ D$ ^; B, T if(*p == x)% X5 g* g/ [8 | n! C
{
; i, D, s, U* i return p;
# q7 P0 t" s) y `! z$ N- h5 h }# C9 S4 d0 G U* q, \# s
++p;/ @1 K& _6 u% k9 ^( e; K9 y! t& B
}
- P( m5 x2 J( Q& F return 0; }
+ G( r2 ?% }0 w+ U/ `& S/ z9 x4 c9 ]" \" n8 T: w+ d2 y! {- {
NO2
! r4 H$ g. y0 g( W. c( x; V9 v6 f4 q2 _1 f2 ?% C8 u& N5 l
Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)) U; M& r) e, x |# b! O
template <class T>3 _. Y: C5 I! v1 @3 h
class Stack
9 j* e0 C$ J7 j. ^" r{: Y4 J3 l: X- J& p% e
public:
- g5 _8 k- a/ R" h7 f' f% Y' Y Stack(int = 10) ;
) V" Q& Q: }" \5 o% D ~Stack() { delete [] stackPtr ; }
- y r/ T0 w) H5 B$ V) ^9 V int push(const T&); 1 l: O" M- a" O9 \
int pop(T&) ;
5 ]; Z! Q) D# v7 f int isEmpty()const { return top == -1 ; }
& K6 @5 d& C6 \) a6 O8 d1 f" h) r int isFull() const { return top == size - 1 ; } * ^# P, j; _) K
private:
4 b5 H; ^0 h, |; @: v7 a. D int size ; // number of elements on Stack.
`: `2 p0 f. t% P, j9 L int top ; / r6 B. S. h/ N0 U+ r J1 t7 u# P9 j
T* stackPtr ; / I9 P3 a8 ^2 c o( E- K5 Z0 X
} ;5 G4 e; K- p& n1 q- v
9 Z/ q4 l4 j$ o% P; v! V! p
+ X0 ~, U1 [4 w* c# v1 S, q- x7 ?' kNO3
0 |" [1 b4 Q. x0 O0 C0 G2 }, M& G; g$ }0 q' V+ Z, c* Z3 f
Implement the simplest singleton pattern(initialize if necessary).. V- J- r6 m3 [% Q3 a( S, \
class Singleton {
7 H' x3 K" j' l. k4 @0 ]- cpublic: 7 `2 e8 }2 S1 l6 [
static Singleton* Instance();" `6 J' S" U. @" ~0 }8 u) j
protected: 9 S1 J# x' A& c* u
Singleton();
* \, Y5 ^- c1 mprivate:
% {( ~2 a& {' \8 H" _ A$ D- T static Singleton* _instance;
8 X: j5 G& G; k! u7 @9 Z}4 `/ B- _$ p+ D3 c
( T B5 C# f( V3 w, J// Implementation
; E+ \. \! S5 t* lSingleton* Singleton::_instance = 0;& o4 y! V9 |) |. N, w5 c. N5 y4 e
+ C% ]$ O, Q' e) q5 o( B( B
Singleton* Singleton::Instance() {
2 {1 _3 E! [% o1 c& G: U/ J if (_instance == 0) {* r% v+ s1 M n' r8 h% _
_instance = new Singleton;
$ ?; l n3 |0 k8 |4 m- `$ d }
6 }4 \8 b- k1 g# C1 X% y return _instance;
+ p, W0 m9 D; P3 t' h}, w0 A5 u; q& K- z
3 j I5 r4 `& x# x u( R* z" E1 ~; M! W: |5 o, ^: x, P
: S3 ?! w: s2 `7 o: e: e
NO47 C$ b8 [4 \' I% O, r4 I
. Q! U% O2 f/ Q' T6 D p( v: T1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
" I! E; i2 y4 a2 q1)Totally 10 coins! x' n6 d t) }0 G/ G) h2 X. x J
2)One can take away 1,2or 4 coins at one time by turns
5 U2 V' m9 S3 \3)Who takes the last loses.
, Q# c& C e! `- mGiven these rules Whether the winning status is pre-determined or not
: }0 y0 i3 o) e" P
* F# K6 G- U3 `% `- \3 z2 O
, p% @8 f+ g2 W7 T1:从后面开始考虑,最后肯定要留1个才能保证自己赢8 K' \" D% O- Z) V( v
2:所以要设法让对方留下2,3,5个
$ G/ _5 H. T' d; S' B. O3:也就是要自己取后留下1,4,6,7,8,9
' I# R+ \! t! k1 ~/ i3 _; x4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
$ a8 u( o1 D8 O5 Q8 | w" u3 p5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
1 t1 O8 E5 m _6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
" s/ K+ L, Z: f! h% D7:所以很显然,我只能抽剩1,4,7
# L: V( N% U' ]8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对% _; {2 v( Y; E( q" m$ s
方先抽,也即是先抽的人输
! r- Y% Q1 }3 H7 d- {/ B4 m {# s" y/ ^, E
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
8 {! V4 r; `# J: J- A1 b8 x2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html |
|