2013年下半年程序员下午真题试题分析与解答

0
收藏   分享
  • 卷面总分:75分
  • 试卷类型:真题考试
  • 测试费用:免费
  • 答案解析:是
  • 练习次数:143次
  • 作答时间:150分钟
试卷简介
2013年下半年程序员下午真题试题分析与解答
试卷预览
1

  阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。

【说明】

    某应急交通控制系统(TraficControISy ,tem)在红灯时控制各类车辆(Vehicle)的通

行,其类图如图5-1所示,在紧急状态下应急车辆红灯时也可通行,其余车辆按正常规

则通行。

下面的C++代码实现以上设计,请完善其中的空缺。

 

#include 

#include 

using namespace std;

class Vehicle {/*抽象基类,车辆*/

public :

virtual  void  run ()  =  0;

};

class Emergency(  /*抽象基类,可在红灯时通行的接口,函数均为纯虚函数*/

public:

(1)=0  //isEmergent()函数接口

(2)=0  //runRedLight()函数接口

};

class Car: public Vehicle {

public :

  -car(){  }

      void run () {   /*代码略*/ }

};

Ciass  Truck:public vehicie  {

 Public;

  -Truck(){  }

 Void  run() { /*代码略*/}

};

Class  policecar:  (3) {

Private:

    bool isEmergency;

public :

     PoliceCar ()   :  Car () ,  Emergency ()   {  this=>isEmergency = false;

}

PoliceCar (bool  b)   :  Car () ,  Emergency () {  this=>isEmergency = b; }

  ~PoliceCar ()   {      }

     bool  isEmergent ()  {  ret irn    (4)  ;   }

     void runRedLight ()  {  /*代码略*/ }

};

/*类Ambulance. FireEngine/*实现代码略*/

class TraficControISystem {/*交通控制类*/

private :

Vehicle*v[24];    int numVeh: cles./*在构造函数中设置初始值为0*/

public:

  void control(){  //控制在紧急情况下应急车辆红灯通行,其他情况按常规通行

for (int i = 0; i<[numVehicles;++]  {

    Emergency*ev=dynamic_cast

if (ev !=0(5)-: runRedLight ( ) 

Else    (6)-:run()

}

}

Void  add(vehicle){v[numvehicles++]=vehicle;}

               /*添加车辆*/

Void  shutdown(){for (int  i  =0;i

}

int main ()  {

    TraficControlSystem* tcs =new TraficControlSystem;

     tcs->add (new  Car ()  );,               t cs->add (new  PoliceCar ()) ;

  tcs->add (new Ambulance ()) ; tcs- >add (new Ambulance (true)) ;

tcs->add (new  FireEngine (true)) ;  -.cs->add (new  FireEngine ()) ;

tcs->add (new  Truck ()) ;

tcs->control () ;                  tcs->shul Down ( ) ;

     delete tcs;

}

1

  阅读以下说明和Java代码,填充程序中的空缺,将解答填入答题纸的对应栏内。

【说明】

    某应急交通控制系统(TraficControIS,stem)在红灯时控制各类车辆(Vehicle)的通

行,其类图如图6-1所示,在紧急状态下应急车辆在红灯时可通行,其余车辆按正常规

则通行。

 

    下面的Java代码实现以上设计,请完善其中的空缺。

【Java代码】

abstract class Vehicle

public Vehicle ()  {   }

abstract void run

};

interface Emergency {

         (1) ;

         (2) ;

}

class Car extends Vehicle {

      public Car ()  {   }

       void run () {   /*代码略*/  }

};

class Truck extends Vehicle  {

      public  Truck ()   {    }

       void run () {   /*代码略*/   }

class PoliceCar   (3)

boolean isEmergency = false;

public  PoliceCar ()  {   }

public PoliceCar(boolean b) {  this . isEmergency =b;   }

public boolean  isEmergent ()  { return    (4);   }

public void runRedLight () {   /*代码略*/    }

}

/*类Ambulance. FireEngine实现代码略*/

public class TraficControISystem  {/。交通控制类。/

    private  Vehicle[ ]V=new  Vehiele [24];

    int numVehicles;

    public  void  control()  {

    for (int i=0;  i

    if  (v[i]   instanceof  Enu rgency&&  ((Emergency)V [i])

    isEmergent())  {

  ( 5 ) . runRedLigh': (  ) ;

  }else

(6).run( )

}

}

void add (Vehicle  vehicle)  {  v[numVehicles++]=vehicle;)/*添加车辆*/

void shutDown()(/*代码略*/}

     public  static  void main (Stri.ng [ ]  args)   {

          TraficControlSystem tcs = new TraficControlSystem() ;

              tcs . add (new  Car () ;

              tcs .add (new  PoliceCar ()   ;

              tcs .add (new  Ambulance ()   ;

              tcs . add (new  Ambulance (true》 ;

              tcs . add (new  FireEngine ( true》 ;

              tcs . add (new  Truck () ;

              tcs . add (new  FireEngine (   ) ;

              tcs . control () ;

              tcs . shutDown () ;

      }

}

1

试题三(共15分)

    阅读以下说明和C代码,填充代码中的空缺,将解答填入答题纸的对应栏内。

【说明1】

    下面的函数countChar(char *text)统计字符串text中不同的英文字母数和每个英文字

母出现的次数(英文字母不区分大小写)。

【C代码1】

Int  countchar(char*text)

{

int i,sum=O;    /*sum保存不同的英文字母数*/

char *ptr;

int c[26]={0};    /*数组c保存每个英文字母出现的次数*/

/*c[0]记录字母A或a的次数,c[1]记录字母B或b的次数,依此类推*/

  ptr=(1);    /* ptr初始时指向字符串的首字符*/

while (*ptr)  {

    if  (isupper (*ptr)  )

    c[*ptr一’A’]++;

    else  '

    if  (islower (*ptr)  )

    c[*ptr一’a’]++;

(2);    /*指向下一个与字符*/

}

for (  i=0.  i<26;  i++  )

 If(3)sum++;

return sum;

}

【说明2]

    将下面C代码2中的空缺补全后运行,使其产生以下输出。

    f2: f2:f2:2

    f3: f3:1

【C代码2]

#include   

int  fl (int  (*f) (int)) .

int f2 (int) ;

int f3 (int) ;

int main ()

{

Printf(“%d\n”,f1(4))

Printf(“%d\n”,f1(5))

return 0;

}

int  fl(int  (*f) (int)  }

{

    int n=O;

/*通过函数指针实现函数调用,以返回值作为循环条件*/

While(6) n++

return n;

}

int f2 (int n)

{

    printf ("f2:  ") ;

     return n*n-4;

}

int f3 (int n)

{

printf ("f3:  ") ;

return  n-1

}

1

  阅读以下说明和C程序,填充程序中的空缺,将解答填入答题纸的对应栏内。

【说明】

    正整数n若是其平方数的尾部,则称n为同构数。例如,6是其平方数36的尾部,

76是其平方数5776的尾部,6与76都是同构数。下面的程序求解不超过10000的所有

同构数。

    已知一位的同构数有三个:1,5,6,因此二位同构数的个位数字只可能是1,5,6

这三个数字。依此类推,更高位数同构数的个位数字也只可能是1,5,6这三个数字。

    下面程序的处理思路是:对不超过100 00的每一个整数a,判断其个位数字,若为1、

5或6,则将a转换为字符串as,然后对a进行平方运算,并截取其尾部与as长度相等

的若干字符形成字符串后与as比较,根据它们相等与否来断定a是否为同构数。

【C程序】

#include

#include

#include

int myitoa (int,  char*);    ,/*将整数转换为字符串*/

/* right取得指定字符串尾部长度为ler gth的子串,返回所得子串的首字符指针*/

char *right (char*,  int  length);

int main ()

{

    int  a, t;        int  len;

   char as [10],  rs[20];

printf(”[1,10000]内的同构数:\r”);

for (a=l.a<=10000; a++)    {

  t=  (1);    /*取整数a的个位数字*/

  if (t!=l&&t!=5&&t!=6)  :ontinue;

  len=myitoa (a, as)j    /*数a转换为字符串,存入as*/

  myitoa (a*a, rs);    /*数a的平方转换为字符串,存入rs*/

/*比较字符串as与rs末尾长度为len的子争是否相等*/

    if(strcmp(as,__(2)  )**o)    /*若相同则是同构数并输出*/

    printf(“%s的平方为%s\n”,as,rs);

  }

  return O;

}

int  myitoa (int num,char*s)    /*将整数num转换为字符串存入s*/

{

    int i,n=0;

    char ch;

/*从个位数开始,取num的每一位数字转换为字符后放入s[]*/

    while (num)  {

    s[n++]  =(3)    +’o';

    num=num/10.

  }

  s[n]=’\0 ‘;

    for (i=0;  i

    (4)  ;  s[i]=  s [n-i-l];s[n-i-1]=ch;

}

return n;    /*返回输入参数num的位数*/

}

char *right (char*ms,  int  length)

/*取字符串ms尾部长度为length的子串,返回所得子串的首字符指针*/

{

int i;

For(;*ms;ms++)    /*使ms到达原字符串的尾部*/

for( i=0;  i

return ms;

}

1

  阅读以下说明和C函数,填充函数中的空缺,将解答填入答题纸的对应栏内。

【说明】

    下面的函数sort(int n,int a[])对保存在数组a中的整数序列进行非递减排序。由于该

序列中的元素在一定范围内重复取值,因此排序方法是先计算出每个元素出现的次数并

记录在数组b中,再从小到大顺序地排列各元素即可得到一个非递减有序序列。例如,

对于序列6,5,6,9,6,4,8,6,5,其元素在整数区间[4,9]内取值,因此使数组元素b[O]~b[5]的下标O~5分别对应数值4~9,顺序地扫描序列的每一个元素并累计其出现的次数,即将4的个数记入b[0],5的个数记入b[l],依此类推,9的个数记入b[5]。最后依

次判断数组b的每个元素值,并将相应个数的数值顺序地写入结果序列即可。

对于上例,所得数组b的各个元素值如下:

    那么在输出序列中写入1个4、2个5、4个6、1个8、1个9,即得4,5,5,6,6,6,6,8,9,

从而完成排序处理。

【C函数】

void sort(int n,int a[])

(   int *b;

    int i,  k,  number;

int minimum=a[0], maximum=a 0];

/.minimum和maximum分别表示数组a的最小、最大元素值*/

For(i=1;i

    if ( _(1)  ) minimum = a[j];

   else

       if ( _ (2)  ) maximum = a[i];

}

   number = maximum - minimum + 1;

    if (number<=l) return;

b  =   (int  *) calloc (number,   sizeod (int) ;

    if  ( !b)   return;

    for(f=0;i

    k= a[i] - minimum;    ++b[k];

}

/*按次序在数组a中写入排好的序列*/

    l=    (3)  ;

    for(  k=0;  k

    for(;    (4)  ;一一b[k]  )

    a[i++】=minimum+    (5)’  ;

}