找回密码
 加入后院

QQ登录

只需一步,快速开始

搜索
查看: 1153|回复: 0

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

[复制链接]
发表于 2012-3-28 17:45 | 显示全部楼层 |阅读模式
一些笔试题目和整理的答案 - 腾讯(Tencent)
) u2 ]' W" J) e0 N, ^9 Q% L. B/ O2 s; U# W
" k4 s4 N8 J6 \
NO1' v6 S& Y# x. o$ y) ~+ l4 A" L0 M
Below is usual way we find one element in an array" K2 v* z  C# C- v6 A
const int *find1(const int* array, int n, int x)0 J& t2 K. S. n! J( }( x
{  s) h, s+ o. \' f2 G" K$ z
    const int* p = array;1 F6 w# }% F, K7 v5 y1 O  v
    for(int i = 0; i < n; i++)
: m; H4 f+ I+ _" c2 _    {
) R+ N: L- S+ {% V        if(*p == x)% f4 F3 ^0 L% l( I( ^0 }
        {9 c  {% o( a1 q3 h5 a6 t1 t! ^, T
            return p;  j8 G. s# C0 I" v- v- C5 }* T: X# O
        }
) `% M; h3 ]7 g2 \3 H        ++p;, j( f- Z' g; ?  P. p
    }
& [* b* t3 ?8 C    return 0; }
1 @7 l1 H5 Y7 D# R% s$ AIn 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 e8 t. v: ]9 _; B1 l& }0 N/ L9 N( q
% I6 L# I7 }9 n$ Ttemplate <class T>; b. i' W, r' P" r
const T *find1(const T* array, int n, T x)9 G% T. |# w- a7 |0 n
{
; `) s2 T" b9 C0 K) S    const T* p = array;
" L4 t) Y2 s1 B% m1 B9 t    for(int i = 0; i < n; i++)5 i  ?3 B6 v* M" q
    {
1 V! z; D4 ^+ z        if(*p == x)
4 i4 ^2 T6 F/ H. H2 _$ K        {7 S1 {" T4 Z- W% o
            return p;7 I: h% I! x8 ]. ~8 m7 `. A1 [/ ?
        }& m( s. m7 X8 V1 y3 }
        ++p;! ~& I; q( y* R* [- Y
    }3 q" U) p) d3 W7 X
    return 0; }) m: ]2 I& }. U" W+ A1 F, l+ q" S3 v$ _4 B

" J* p5 I' u$ _" K1 YNO2& j, b' f% w7 A! X! [4 [" i

& w8 b; f4 ]( A) T6 [3 qGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)1 g: b/ j8 s% N4 s) ]0 O! O) \
template <class T>/ h# X1 X' N( p3 |9 O& {
class Stack8 Q8 G! [( P& |) \, h7 [
{( C0 z+ Q) l0 d  q+ S8 b* j) s
public:5 [2 [6 F6 d( }' s' s9 r0 U
       Stack(int = 10) ; 5 @9 S" @1 l! g$ f- @7 i
       ~Stack() { delete [] stackPtr ; }7 R( y' `, C( j
       int push(const T&);
! H' \- V$ b" `: c       int pop(T&) ;  ( N, W! w) y6 G( ~
       int isEmpty()const { return top == -1 ; }
4 d% r" ]: ?8 c1 i5 e       int isFull() const { return top == size - 1 ; }
, u  S' U+ x' x' G) |1 C( `6 G* Rprivate:
6 @4 v) U& f# j2 _1 P1 r/ r" d       int size ;  // number of elements on Stack.# s0 [) C1 u2 k) Q4 T- F
       int top ;  
: Q6 X$ J+ C6 ?( k" b       T* stackPtr ;  9 q7 \6 x- ~4 S  _( ^* y5 _0 Y) Z
} ;
) A3 x& S  t* q) x) f
- C( B0 `9 ~- c; e% P
  M3 B( {9 f5 |* B! |NO3
6 u; Y- D- J# @+ Z) M+ f, @) ^0 V2 i1 E6 k% `
Implement the simplest singleton pattern(initialize if necessary)., X- V5 t2 j1 ?1 a: A! b
class Singleton {
& W0 ]& O) F) ^6 ^" v6 z7 kpublic: ; W1 y! P( C$ S9 i  s
    static Singleton* Instance();
8 j; n6 X  I' W4 n, P% Y$ N; w+ nprotected: 4 U" Y& b3 x# B- M% k5 F/ A
    Singleton();
, ~% ^# O. L6 fprivate:
8 O+ ]7 O: b/ y5 i( g! M9 L5 Z( E3 O: Y    static Singleton* _instance;
+ L8 G9 S0 v. |}
" \( e5 l9 G. `; M4 r) c5 ?  R6 j5 z+ j% r0 K6 n
// Implementation 7 X* l7 e, r# H6 P9 [
Singleton* Singleton::_instance = 0;2 S) ]4 ?5 M1 y/ O4 R
6 ?) e. U" W. r: T* M& e/ U" J3 Z
Singleton* Singleton::Instance() {( r9 `: b: g2 P% \7 D; |
    if (_instance == 0) {
7 M5 B* q; k* C8 m        _instance = new Singleton;6 d7 Z- g" p; J# F
    }
; i& s+ x4 T' I: p+ W  R& @( H' o    return _instance;
; p% m7 Q% R0 f1 s) J0 i}0 B( S+ W3 z( O/ ]8 g& Q( r  v

! m/ a; \: k0 y/ i3 ?, j7 O4 e0 F9 h3 u

: }) V) V& K  A1 D' B! s6 G" @NO4
6 _: \- g9 q' }9 O  J; v& v1 ^, q9 U  K  a. p  }. z1 j4 [
1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:+ u" v4 K9 Z2 |% K' m, ]+ H- O9 f
1)Totally 10 coins) r, C! X0 J/ X4 @1 B  n! q" U# w
2)One can take away 1,2or 4 coins at one time by turns% f4 [$ \' A. t2 m4 X! h6 Z, i
3)Who takes the last loses.
7 T# {/ ~$ d, ~6 [8 p* BGiven these rules Whether the winning status is pre-determined or not1 `. g' V% n& C# {& N2 P
7 D9 b" E8 q7 u: J
' H7 ~# @" E) f( Z$ u
1:从后面开始考虑,最后肯定要留1个才能保证自己赢
) I( ^0 l) @/ _/ c! r2:所以要设法让对方留下2,3,5个* `$ i' C" E6 ~
3:也就是要自己取后留下1,4,6,7,8,92 _. y1 ]9 ~& a; j1 u0 W8 b5 E7 v7 _
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6
" _9 F* [. ~0 H4 v( K5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8, P, ^) B* l1 O2 ~8 y% X8 V/ M5 l
6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除' ?: A9 w2 }$ ^/ d3 B- Q! q
7:所以很显然,我只能抽剩1,4,7
- c2 \- o  V: j) |5 ?: U8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对
9 ]5 h% w) m+ _5 ^2 V方先抽,也即是先抽的人输! n! h( ^0 [+ M( `2 z
6 @1 f9 A% [* H- x
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
6 \- K9 T" d" ~0 N8 A( V; G2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

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

GMT+8, 2026-9-23 21:17

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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