找回密码
 加入后院

QQ登录

只需一步,快速开始

搜索
查看: 1140|回复: 0

[其他] 一些笔试题目和整理的答案 - 腾讯(Tencent)

[复制链接]
发表于 2012-3-28 17:45 | 显示全部楼层 |阅读模式
一些笔试题目和整理的答案 - 腾讯(Tencent)3 W" g4 u. f5 ?, M( E0 X+ d' p
: B8 X% J+ i5 _5 r
( _7 ^3 b! K9 `* W5 P: X- D
NO1, ~5 J3 z# @& e4 V- H1 Q( v
Below is usual way we find one element in an array
9 J* s0 n- c8 [; lconst int *find1(const int* array, int n, int x)3 _1 v! r- N7 L! O
{* E4 ]+ f4 y' L& I" \
    const int* p = array;
7 N5 _+ s7 t" E% c    for(int i = 0; i < n; i++)
. X: F( j' M+ r5 e% U: }    {1 W6 C; d" w8 N, W- m
        if(*p == x)( X$ l9 n# u7 X+ B
        {
9 t0 e% d( N9 V( ~5 @4 g% a            return p;
; B! }. M5 |) ~9 m  C! R1 Z+ \4 k        }0 b) C; y* \, U$ u) Y' L
        ++p;5 B6 x6 g2 r: D! r& p! |
    }# }# h  k9 R' E5 m
    return 0; }
4 U% f, V/ q; w* RIn 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?
9 X6 _; W( p8 |. B7 D/ W
8 O2 ~) J5 B0 Z( f% s0 [  ztemplate <class T>
6 B" W& ~- v( j) C0 r; Fconst T *find1(const T* array, int n, T x)
- G! |" O1 Z% w: ~! u2 Q{
7 e! g1 g6 N1 Z6 ^2 D    const T* p = array;9 a) a+ x! ~# S. T: M% o7 n+ i( L
    for(int i = 0; i < n; i++)
4 }) V- B4 `& D; P    {$ e" `4 b5 y5 J9 P& E1 \0 b
        if(*p == x); {0 y! h) L& A, K2 ?. l
        {, v# h! k& j( R4 g
            return p;
. e2 D0 J: u$ W! H4 Q        }: V/ z# Z" D* g0 R$ a
        ++p;
* x2 ~( f, C+ d4 M    }; L) c/ G4 V, r: U
    return 0; }
, T; f9 N1 s6 [6 j# c, A& d% D8 X; m2 J" A8 G6 n3 D
NO2) z  H* h: w4 {) F2 X7 \% s

0 E! A- A8 q- I3 h- w5 Z4 XGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)& e9 ^- u2 _; t4 s( m
template <class T>; l/ t/ ^; S  r/ c( l" F9 B: G
class Stack
( E4 r& C( P( V( R' }% ?0 h{# H0 `* \5 @" Q! [: x3 t% o4 ?
public:
3 E8 }! K0 t, |0 q' ]       Stack(int = 10) ; 0 S8 V7 I2 @5 E$ `4 R
       ~Stack() { delete [] stackPtr ; }
/ c& H/ d+ H4 E/ E# J       int push(const T&); " h5 j6 s! f# }9 e
       int pop(T&) ;  % @/ x! `' [% }- F
       int isEmpty()const { return top == -1 ; }
5 L  p$ Z! W8 @, `5 c; [       int isFull() const { return top == size - 1 ; } & \2 m9 L0 g4 v0 Z/ u& ?+ I
private:
0 P+ E3 r$ d- z: ^$ }+ m" v$ @: H3 \# k       int size ;  // number of elements on Stack.$ K6 z" [, t# ~
       int top ;  . d1 z4 w8 w4 J7 t2 [
       T* stackPtr ;  6 A/ A! w1 l: L) f+ l
} ;* I+ ]* y6 X: i/ f' c
! y3 \: e1 e. b( w1 A; h8 v3 y

2 V; u+ u  j# k( A0 R1 W% KNO3/ X0 \) u2 U! y" G* b
  {$ Q7 z. a' M! j* y) i) W) J
Implement the simplest singleton pattern(initialize if necessary).
& d4 i' u6 Q0 V6 }) `class Singleton {
2 k5 \3 Q9 J- c% r; A8 D+ Lpublic:
6 Z4 T# g+ t. Q$ U+ `    static Singleton* Instance();
. V) x, Q0 K- `protected:
% n' X+ W- W3 {. k2 O- q( `    Singleton();
+ J5 |+ O$ A% kprivate:
7 z' e& G- ?" S3 K    static Singleton* _instance;2 Y* T; ?" b  A
}0 F% Q( S8 B( I3 L
, q  s7 R  c& Z9 c  }' d" W
// Implementation
1 k! M! J( A# ^5 MSingleton* Singleton::_instance = 0;
2 n9 d0 {0 x: l: w, Z! b/ j- K0 W+ O3 A6 _" p2 L5 I( p
Singleton* Singleton::Instance() {) ~8 l0 `+ M4 G% s3 Z4 i
    if (_instance == 0) {  l+ H. {- D, ^% k9 ^9 O: n
        _instance = new Singleton;
" H# v$ X: m, s0 Y9 x% i    }1 x9 x7 E4 b7 f: S
    return _instance;3 p& K) r" p, a. u4 P% p
}
6 x! w2 n! `3 H$ W2 R- v* w3 s* \
; T2 W7 g% K; s1 _) v! w
- ?% R7 _7 t' l/ [( U' ^# X' x6 o; m6 {
NO46 a6 \& r. J' `- A& T
' I& Z+ \, K1 o% |
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
  u) ]5 n" k, Q4 k/ Q1)Totally 10 coins
9 t# N5 n6 V9 J! F2)One can take away 1,2or 4 coins at one time by turns0 F" N; u+ K# m: q& J
3)Who takes the last loses.3 ^# z% |3 D6 W, b! F# V
Given these rules Whether the winning status is pre-determined or not  ]  T+ q3 n3 E% r

3 ~6 a8 D& s1 `) N; C7 ^! \: q; Z2 ^. s. f6 i
1:从后面开始考虑,最后肯定要留1个才能保证自己赢
: ]1 L  p0 o/ |0 _2:所以要设法让对方留下2,3,5个' w& v: T% w. L! ~4 V( P9 b7 k
3:也就是要自己取后留下1,4,6,7,8,9
. S, L( H5 Z' l! a' Y5 {4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
% ^0 n( _' E  F. l5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
( Z8 l3 r/ S3 C5 H6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
6 H$ \  Z8 }5 u4 @/ p2 Y; v7:所以很显然,我只能抽剩1,4,7
/ U9 q( L% B' v8 c6 ~5 c+ X8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对' }# g% E& P5 H& V% u. h
方先抽,也即是先抽的人输
0 M; Y; t+ X, q& x# K9 c% ~6 ]) }* l
5 z7 C# V0 u! {7 w腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
. r* E/ s( ~& I7 N. U: |2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2026-7-27 03:05

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表