找回密码
 加入后院

QQ登录

只需一步,快速开始

搜索
查看: 1108|回复: 0

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

[复制链接]
发表于 2012-3-28 17:45 | 显示全部楼层 |阅读模式
一些笔试题目和整理的答案 - 腾讯(Tencent)
5 `0 `- T! U) e& S' [
2 S* C! Y; }( d& z; M% R8 o2 S, n$ p
# z( g, ]; L1 ~  CNO1
9 `4 B. m# _. F$ zBelow is usual way we find one element in an array
% p* L. F; ~) p  fconst int *find1(const int* array, int n, int x)
' L4 b4 o1 e7 {7 ], Z{" h; r! c# E7 S2 U
    const int* p = array;9 \2 U  S1 j7 U; y, K" A0 {
    for(int i = 0; i < n; i++)
9 l$ V* [/ ?, H! `$ c6 g    {
8 K% V" i) h- {9 X, v        if(*p == x)& Z& m# o6 Y, R& g% _
        {5 S4 g2 g  G# _
            return p;6 x6 U$ z  o, S1 t) D* X
        }/ B0 o6 n3 T! d7 q- a5 b
        ++p;
2 V: k) ?1 B, R5 a8 z    }. Y& z- Q) e: \# v& x  l1 J9 y8 d  E2 x
    return 0; }4 O7 u+ z0 H4 U0 h) j) Z' R' x* h
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?9 w7 N) G; y1 M1 ^( h" H1 n5 i
9 Q8 i+ p! O6 j# A% ]1 f
template <class T>
# ~3 Q) X. H# C; O* K6 Zconst T *find1(const T* array, int n, T x)
4 e6 S- T; P) h+ {{
6 z& f/ Y& _, w    const T* p = array;& ~& ?6 \: O& C$ `/ w0 D9 @
    for(int i = 0; i < n; i++)  I5 {8 k0 G: y4 k5 u" K  c6 e
    {1 y4 k- [, H6 k6 I6 z8 c+ m
        if(*p == x)$ F8 Z9 T: S1 N) O* l  z) z# R, L) x
        {
3 t* y3 P) W2 }% E0 n5 q+ L5 J' M            return p;- ~# ?  I2 U2 c! P
        }
* y# _( P6 c- a1 T        ++p;
2 d' f" U, ]6 ]9 h+ e    }' P) k) ?* y- r# i3 ?; n& {
    return 0; }
% [9 c. g2 i' g$ \' E/ V# d
) F8 z( w) J$ S! `& oNO2
" J! |0 X8 {( e. W! P8 {: ]: ]2 D
Give an example of implementing a Stack in the template way(only template class declaration without detail definition and realization); X! c: f  A6 g) k$ Q$ K8 B5 m. ?6 ~
template <class T>6 z8 F$ `( y' F+ l* u' y2 z
class Stack
* Y8 B5 e9 q( s& B; G{
( u# Z) E" h8 t9 |+ B4 rpublic:: ~9 t+ _) N3 Q) S. p0 X' {
       Stack(int = 10) ;
4 c/ G; R" p' |! P- t% A       ~Stack() { delete [] stackPtr ; }
  s* b8 _  G, n8 r8 i       int push(const T&); 7 F) n& g4 L0 s! s% G/ S0 Y' w
       int pop(T&) ;  / z3 p* ?5 G7 M$ y0 Q; B
       int isEmpty()const { return top == -1 ; }
$ F5 k! Z/ W' W, i; F* j0 N       int isFull() const { return top == size - 1 ; }
7 g; N( C& m3 Jprivate:( |# r; c5 b2 S! h4 s7 _
       int size ;  // number of elements on Stack.
: u& N' }/ S2 q3 I5 T5 m       int top ;  
% Z/ _9 \* a$ x       T* stackPtr ;  7 O& G% S% d/ P6 x
} ;! b. U0 O1 t. _0 d5 W
7 ^  @' O/ v- R( C& T' F6 M
3 c+ l; k0 u8 A
NO3
3 p& g3 ~. H/ x! E% N7 `* y/ }
- ^6 v; n1 Q: J) d6 N) C, mImplement the simplest singleton pattern(initialize if necessary).: }* _  `6 D( w2 F/ d0 I
class Singleton {
9 s- {# ^# ?+ Spublic:
% c5 i: a( h2 M3 [0 d2 D: X    static Singleton* Instance();
/ k, n* X6 v0 }0 C9 r$ Xprotected: ' Y6 w1 G% S# q& p& o
    Singleton();
9 V; Q/ K. z& n5 e) V, k7 Pprivate:6 A- i8 X1 A8 ?  U% I
    static Singleton* _instance;
& I# {1 I+ C% i3 ~3 e! L}1 V0 p5 V  ?1 _9 |" S/ T7 ~

) P# F% c6 _7 e' T% Y8 i// Implementation
: i5 \6 M) q3 M, h1 e1 DSingleton* Singleton::_instance = 0;/ Q5 H9 v# W2 T( [' E/ T
& [9 ^7 u6 D7 `8 f# \& _; e
Singleton* Singleton::Instance() {* j& \+ C4 |* ?
    if (_instance == 0) {( a/ o/ K* b+ Q4 N! y! ?2 g
        _instance = new Singleton;
, }7 Q: f0 O9 ~3 F" y" v( y    }  N* r2 n4 l4 ]# I# u2 j3 s1 r  g
    return _instance;
# ^, W, G/ Q, S  d}$ C( U6 X, o9 K+ R' Y1 E
+ F7 q8 p8 {! w3 U! R
. S8 A$ `% h: U& \8 D/ b
. `8 \& |2 v+ X+ ]& v1 u
NO4
0 m6 p! Y; e1 W. J. q
) _7 [8 f8 t* X' [1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:
3 P- [6 J; R4 c& D: s$ q8 O1)Totally 10 coins7 N8 B) w5 ~/ S- r$ j+ N. }
2)One can take away 1,2or 4 coins at one time by turns
+ h4 ~1 U) O( m  k. G3 }3)Who takes the last loses.9 n8 A1 r% r" |/ k
Given these rules Whether the winning status is pre-determined or not; o& W% \$ X% T& N

! ^8 L6 C' K) j# z! N, V
" ?( u, C, ~/ J9 O1:从后面开始考虑,最后肯定要留1个才能保证自己赢7 \2 c  Q1 t. w: P' U: l
2:所以要设法让对方留下2,3,5个. w0 ?  m: r8 i% _3 @* P
3:也就是要自己取后留下1,4,6,7,8,91 r0 N1 e9 D: j/ c. S4 t1 y
4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除64 q& x: Z9 `% D5 {
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
5 T! c! N- t3 _1 x# e* i* Q1 i% T. f6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
) N. D  D* L. X: [0 ?* E! j7:所以很显然,我只能抽剩1,4,7! Z' C; N$ j+ y* c8 W$ ~( w* R' _, ~
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对" L9 O3 A4 ]# e6 W6 u6 N3 W2 L
方先抽,也即是先抽的人输
* X0 \3 E* }! x9 w- m( R/ C/ C
腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html
( K; I& F. P5 Q; m' p6 I) d2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

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

GMT+8, 2026-4-2 04:56

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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