工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 844|回复: 0

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

[复制链接]
发表于 2012-3-28 17:45 | 显示全部楼层 |阅读模式
一些笔试题目和整理的答案 - 腾讯(Tencent)
: A5 l0 Z" ~/ H' A0 n) K) @6 V& K5 T( l/ d

6 x; U# s- m* h1 {  Q- v( c3 QNO12 c  o, w: y' E; i7 a" c& D
Below is usual way we find one element in an array6 T0 J) B# J% o* x" E
const int *find1(const int* array, int n, int x)
6 X7 {9 \, `+ ]{: y- M9 ]" X  e" f
    const int* p = array;
* y) y9 Q% K2 e5 R    for(int i = 0; i < n; i++)
0 t1 P$ a  W9 p0 v/ r* r    {  J$ i& X! P" y" u7 {
        if(*p == x)( l6 I+ P6 p. {& x8 `/ B
        {. p' p  n0 C; I# V3 E$ U  r
            return p;/ `& a! o: E, J4 W
        }
' b0 }1 M: T/ ]( e. B        ++p;
  d  F7 Q- C9 |+ s+ n" d    }" O7 b1 k) g- ~  t1 Z
    return 0; }  @+ f, Q! D: b* u2 V# m
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?
8 n# B) {1 _3 }- [% T$ n6 r3 T( O( Z5 e1 t- W0 I3 |, _
template <class T>. b( Z4 F' B" d0 ]
const T *find1(const T* array, int n, T x)
& ^& F. x( m# r! `' F1 H. _' @! M{* g( d1 t5 e; b, d
    const T* p = array;
1 g, @+ u9 m) l; ]2 {/ p    for(int i = 0; i < n; i++)6 \) u( ~6 F& C3 ?2 C* f
    {
" P5 w) Z5 q& M7 u' d        if(*p == x), h- ]" \6 Z3 E* b& {. L
        {
$ T7 [: \7 b( n+ K" z* V            return p;
8 r  J! H7 H3 H- r+ \* X2 g        }
! z2 q% x' m+ T3 s( R        ++p;3 r4 L9 M4 Y; G1 B
    }
/ K! m7 l! }% U0 u    return 0; }
3 I3 }1 L5 U/ V3 j  |1 w$ ~: _7 W4 }0 b# X( \0 w
NO21 Z6 O. u& t( K* p. a

7 O8 Q5 a/ Q( B) s! mGive an example of implementing a Stack in the template way(only template class declaration without detail definition and realization)5 Z+ V) z/ g5 c$ e$ S$ b# e, l$ o
template <class T>' i0 t. X  k; t
class Stack
8 l+ w5 b  e3 v" ?8 ~{
: O, a& U" H& Y* y  Cpublic:
( v# x( z9 s- m; P. h) }4 h% a' a. N       Stack(int = 10) ; ; u4 E. B( }2 f3 }
       ~Stack() { delete [] stackPtr ; }. r( @% l  [5 n1 ~
       int push(const T&); 9 S: S# N1 m& h
       int pop(T&) ;  
  f0 P& t- }5 p1 d% Z" ]* F2 H+ X       int isEmpty()const { return top == -1 ; } ! n' k+ w1 N3 R$ M" s
       int isFull() const { return top == size - 1 ; } 2 {7 I# R9 G! F& Z! R' w) a2 {$ c2 K: l
private:
$ ?- d4 n$ j  \9 a! a* F/ F" U       int size ;  // number of elements on Stack.
* S) l4 ?3 Q1 G2 c* I  z+ ]& N       int top ;  * Y* l- x) r6 Z: P8 w7 @
       T* stackPtr ;  
. L5 I, @( |5 c7 x/ K, I9 \9 e+ Z} ;
3 s5 {2 ^# f) _; M4 l- f5 u) o1 R  l4 r! R

8 }" r  W* \2 R0 ~. X' DNO3
7 K/ F, G, R2 M& ?9 A! v
8 E& C/ y" W' V7 x/ P5 a7 `' M& lImplement the simplest singleton pattern(initialize if necessary).
/ R. C- z* I6 I1 M4 Gclass Singleton {( ^8 ~: W1 |3 ]& E5 U
public: 3 k# _; v3 J- B
    static Singleton* Instance();( a% _& o( E+ f
protected: ! ^4 q$ h2 l3 j& j) R) \2 b# h6 G
    Singleton();
1 O  t# T) H% w' v% Zprivate:: N! s( k. x. n8 l1 n1 T) s1 B
    static Singleton* _instance;
9 [& I6 J8 ]6 m( f1 G. x+ C2 v}5 V0 X: P8 `# X# z2 v1 K

' [' N: X% H7 e% I. D: U8 K// Implementation * X0 L( x0 ~/ W9 x
Singleton* Singleton::_instance = 0;! C/ [3 i' V+ I! r" K2 B

4 Z8 ?  ~2 X% YSingleton* Singleton::Instance() {6 z% @" t' z# H  [% v" A3 J
    if (_instance == 0) {1 ~4 p: t0 W, m8 D2 S7 S
        _instance = new Singleton;) B, s0 l) l6 v9 f0 z
    }- m2 o) K; r4 h& c' W' k
    return _instance;! |+ @7 A: ?/ k# O  v9 |# e9 C" @
}  x& r2 ?0 N/ P% c

) r( |" w3 X+ ?# Q$ e
2 s3 D; y* H4 R9 c- K2 ]
# j! `' k4 D9 s9 x2 B; J9 K" W9 b! eNO4
& x) J/ x. ~9 Z) A) ~
* Z; N8 N0 H/ C0 l1.Jeff and Diamond like playing game of coins, One day they designed a new set of rules:6 s/ H) f5 G1 C1 k/ }( s
1)Totally 10 coins
, V# z: U! h' u6 a: V/ E9 S$ b2)One can take away 1,2or 4 coins at one time by turns
6 C! [% q* T+ h. i" P8 j/ ~" W/ @3)Who takes the last loses.& d: f: W+ F8 q; {" v
Given these rules Whether the winning status is pre-determined or not
7 U# E  O( k. B) @- ]5 W. I3 p! V) F5 }! }

" H9 ?$ P$ n3 b. O) f0 E' n; {8 y1:从后面开始考虑,最后肯定要留1个才能保证自己赢
2 J, ]4 i7 r+ ^. i2:所以要设法让对方留下2,3,5个) L" \: |7 t/ @
3:也就是要自己取后留下1,4,6,7,8,9
, M+ T; A0 X1 a9 [% {+ U4:如果自己取后留下6,对方取2个,与(3)矛盾,所以排除6$ R4 n: c1 @$ i' i% P, a- E* w2 \
5:如果自己取后留下8,对方取4个,与(3)一样情况,所以也排除8
1 ]! X) g- _" u4 Q6:同样,9也不行,如果我抽后剩下9,对方抽2个,就反过来成对方抽剩成7个了,也与3)矛盾,所以也排除
: y% \+ K! T3 h7:所以很显然,我只能抽剩1,4,7" `. @+ X# i# _5 o
8:因为只能抽后剩1,4,7才能赢,我先抽得话不可能达到这几个数,很显然,只能让对- R! m9 z. ]; O. N$ Y  D
方先抽,也即是先抽的人输/ P7 {9 M: j/ J; e7 |3 p. p

2 G' E# b0 P& g8 m' x& Y, |  ]腾讯俱乐部:http://bbs.aftjob.com/group-47-1.html! s# Z- `1 A) K. K6 R
2011年名企薪酬信息专版:http://bbs.aftjob.com/forum-37-1.html
您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

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

GMT+8, 2024-6-7 14:10

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

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