C/C++面試題目集錦

來源:果殼範文吧 8.39K

一、輸入一個n ,然後在螢幕上打印出NxN 的矩陣!

C/C++面試題目集錦

例如,輸入一個3,則

1 2 3

8 9 4

7 6 5

輸入一個4,則

1 ???2 ?3 ?4

12 13 14 5

11 16 15 6

10 ?9 ?8 ?7

參考答案:


#include

#include

#define N 10

void printCube(int a[][N],int n);

void main()

{

??int a[N][N],n;

??printf("input n:n");

??scanf("%d",&n);

??printCube(&a[0],n);

??getch();

}

void printCube(int a[][N],int n)

{

??int i,j,round=1;

??int m=1;

??for(i=0;i

a[0]=m++;

??for(i=n-1;i>=n/2;i--)

??{

for(j=round;j<=i;j++)< p="">

??a[j]=m++;

for(j=i;j>=round;j--)

??a[j-1]=m++;

for(j=i;j>round;j--)

??a[j-1][round-1]=m++;

for(j=round;j

??a[round][j]=m++;

round++;

??}

??for(i=0;i

for(j=0;j

?printf("%3d",a[j]);

printf("n");

??}

}



二、朗訊面試題 :

There are two int variables: a and b, don’t use “if”, “? :”, “switch” or other judgement statements, find out the biggest one of the two numbers.

參考答案:

方案一

int max = ((a+b)+abs(a-b)) / 2

方案二

int c = a -b;


char *strs[2] = {"a大","b大"};

c = unsigned(c) >> (sizeof(int) * 8 - 1);

三、朗訊面試題

如何打印出當前原始檔的檔名以及原始檔的當前行號?

參考答案:

通常使用的就是__FILE__, __LINE__,在除錯函式中利用“%s","%ld",列印就好了。

四、朗訊面試題 :

main主函式執行完畢後,是否可能會再執行一段程式碼,給出說明?

參考答案:

crt會執行另一些程式碼,進行處理工作。


如果你需要加入一段在main退出後執行的程式碼,可以使用atexit()函式,註冊一個函式。

語法

#include

int atexit(void (*function")(void));

#include

#include

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

int main( void )

{

??atexit( fn1 );

??atexit( fn2 );

??atexit( fn3 );

??atexit( fn4 );

??printf( "This is executed first.n" );

}

void fn1()

{

??printf( "next.n" );

}

void fn2()

{

??printf( "executed " );

}

void fn3()

{

??printf( "is " );

}

void fn4()

{

??printf( "This " );

}


五、朗訊面試題 :

如何判斷一段程式是由C編譯程式還是由C++編譯程式編譯的?

參考答案:

c++編譯時定義了 __cplusplus


c編譯時定義了 _STDC_

六、下面這道面試題怎麼做(指標)?

#include


main(){

int c[3][3]={1,2,3,4,5,6,7,8,9};

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

for(int j=0;j<3;j++)

printf("%ldn",&c[j]);

printf("-------------------------n");

printf("%ldn",(c+1));

printf("%ldn",(*c+1));

printf("%ldn",&c[0][0]);

printf("%ldn",**c);

printf("%ldn",*c[0]);

if(int(c)==int(*c))

printf("equl");

}

為什麼c,*c的值相等,(c+1),(*c+1)的值不等

c,*c,**c,代表什麼意思?

參考答案:

c是第一個元素的地址,*c是第一行元素的首地址,其實第一行元素的地址就是第一個元素的地址,這容易理解。**c是提領第一個元素。


為什麼c,*c的值相等?

int c因為直接用c表示陣列c[0][0]

printf("%ldn",*c[0]);語句已將指標移到陣列頭。

int(*c)表示c0的值為1,所以相等。

陣列c的存放空間示意如下:(機器中是行優先存放的)

c[0][0] c[0][1] c[0][2]

c[1][0] c[1][1] c[1][2]

c[2][0] c[2][1] c[2][2]

c是一個二維陣列名,實際上它是一個指標常量,不能進行自加、自減運算,即:c++、c--、++c、--c

都是不允許的';

c: ?陣列名;是一個二維指標,它的值就是陣列的首地址,也即第一行元素的首地址(等於 *c),也

????等於第一行第一個元素的地址( & c[0][0]);可以說成是二維陣列的行指標。

*c: 第一行元素的首地址;是一個一維指標,可以說成是二維陣列的列指標。

熱門標籤