C++笔试题 下载本文

* 

for(i=0;i<19;i++)

{

}

答:

#include <stdio.h>

int main()

{

int maxsize = 19;

char ch = '*';

char *tmp=new char[3];

char *format = new char[30];

char *stars = new char[30];

strcpy(stars,"");

for(int i=0;i<maxsize;++i)

{

if(i<=maxsize/2)

{

int k=(maxsize+1-2*i)/2;

itoa(k,tmp,10);

strcpy(format,"%");

strcat(format,tmp);

strcat(format,"c");

if(i>0) //前10行每次较前一行多**,输出位置比前一行减1

{

strcat(stars,"**");

strcat(format,stars);

}

printf(format,ch);

printf("\n");

}

else

{ // 后9行每次较前一行少**,输出位置比前一行加1

stars[maxsize-1-(2*(i+1)-maxsize-1)]=0;

int k=(2*(i+2)-maxsize-1)/2;

itoa(k,tmp,10);

strcpy(format,"%");

strcat(format,tmp);

strcat(format,"c");

strcat(format,stars);

printf(format,ch);

printf("\n");

}

}

delete []tmp;

delete []format;

delete []stars;

tmp=NULL;

format=NULL;

stars=NULL;

return 0;

}

2.

void fun(int x)

{

if(x/2>0)fun(x/2);

printf("%d",x);

}

求fun(10)的输出结果

答:依次输出fun(1),fun(2),fun(5),fun(10),得到结果12510

3.

#define f1(n) (n)*(n)

int i=5;

int k=0;

k=f1(i++);

printf("%d %d",i,k);

输出结果:

答:7 25

4.下面那个for循环是无限循环

for(int i=010;i==10;i+=0)

for(int i=10;(i++^--i)==0;i+=0)

还有几个忘了

答:

第一不是,因为010表示8进制,i=8;

第二是,因为i++^--i表示数字9与9进行异或运算,确实为0

5.Email relay 和Email access分别用了什么协议?(其他还有很多,略)

答:SMTP,POP3

6.stack data (栈)存在于

A.rom, B .flash C .eeprom D.ram E .none of the above

答:D.ram。这题稍微涉及到一点硬件知识,ROM的全称是Read Only Memory,即只读存储器,flash ,eeprom都是ROM家族的一员,RAM是Random Access Memory的简称,意为随机存取存储器,也就是内存了。不管是堆还是栈都是放在内存里的。

7.

int i;

int x=0x12345678;

unsigned char *p=(unsigned char *)&x;

for(i=0;i<sizeof(x);i++)

printf("%2x",*(p+i));

在80x86pc机器上运行结果?

答:x在PC机上的内存存放顺序为78 56 34 12,高字节在前,低字节在后,因此输出78563412(参见big endian 和little endian)

Sun Sparc Unix上运行结果?

答:说实话,我不知道

8.

char a[2][2][3]={{{1,6,3},{5,4,15}},{{3,5,33},{23,12,7}} };

for(int i=0;i<12;i++)

printf("%d ",_______);

在空格处填上合适的语句,顺序打印出a中的数字

答:*(*(*(a+i/6)+(i/3%2))+i%3)

这题主要是要将输出的序号依次写出一些,如000,001,002,010,011,012,100,101...然后找序号变化规律

9.

void fun(char *a,char *b)

{

a=b;

(*a)++;

}

void main()

{

char s1='A',s2='a';

char *p1=&s1;

char *p2=&s2;

fun(p1,p2);

printf("%c%c",s1,s2);

}

输出结果:?

答:输出结果为:Ab,因为在fun函数里,指针a指向s2存储区(a=b),接着让s2存储区的值加1得'b' ((*a)++),所以s2等于'b',s1不变。

10.写一个strstr()函数

答:中磊笔试题第三题也是这个,就不多写了,可翻到下篇看解答。(参见C++笔试题五)

#include <stdio.h>

#include <stdlib.h>

int main()

{

int i;

char star = '*';

char blank = ' ';

char buf[20] = {0};

int stars, blanks;

buf[19] = 0;

for (i = 0;i < 19; ++i) {

stars = 0;

blanks = 0;

if (i < 10) { /* grow up */

stars = (i << 1) + 1;

blanks = (19 - stars) >> 1;

}

else {

stars = ((19 -i -1) << 1) + 1;

blanks = (19 -stars) >> 1 ;

}

if (blanks)

memset(buf, blank, blanks);

memset(buf+blanks, star, stars);

if (blanks)

memset(buf+blanks+stars, blank, blanks);

printf("%s\n", buf);

}

}

C++笔试题(五)