C/C++面试题目集锦(zz)

C/C++面试题目集锦[搜自www.csdn.net]
Irene @ 2004-10-27 13:55

一、输入一个n ,然后在屏幕上打印出NxN 的矩阵!
例如,输入一个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<stdio.h>
#include<conio.h>
#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<n;i++)
a[0]=m++;
  for(i=n-1;i>=n/2;i--)
  {
for(j=round;j<=i;j++)
  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<i;j++)
  a[round][j]=m++;
round++;
  }
  for(i=0;i<n;i++){
for(j=0;j<n;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 <stdlib.h>
int atexit(void (*function")(void));
#include <stdlib.h>
#include <stdio.h>

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<stdio.h>
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("%ld/n",&c[j]);
printf("-------------------------/n");
printf("%ld/n",(c+1));
printf("%ld/n",(*c+1));
printf("%ld/n",&c[0][0]);
printf("%ld/n",**c);
printf("%ld/n",*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("%ld/n",*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: 第一行元素的首地址;是一个一维指针,可以说成是二维数组的列指针。
**c:二维数组中的第一个元素的值;即:c[0][0]
所以:
    c 和 *c的值是相等的,但他们两者不能相互赋值,(类型不同);
    (c + 1) :c是行指针,(c + 1)是在c的基础上加上二维数组一行的地址长度,即从&c[0][0]
    变到了&c[1][0];
   (*c + 1):*c是列指针,(*c + 1)是在*c的基础上加上二数组一个元素的所占的长度,即从
    &c[0][0]变到了&c[0][1]
    从而(c + 1)和(*c + 1)的值就不相等了

七、定义 int **a[3][4], 则变量占有的内存空间为:_____
参考答案:
int **p; /*16位下sizeof(p)=2, 32位下sizeof(p)=4*/
总共  3*4*sizeof(p)

八、编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。
参考答案:
判断年份是否是闰年,月份的大小月,
月(12进制)、日(与闰年、大小月有关)、时(24进制)、分(60进制)、妙(60进制)

--------------------------------------------------

汤姆逊的面试试题:怎么快速检测出一个巨大的链表中的死链?

http://community.csdn.net/Expert/topic/3343/3343269.xml?temp=.509823

看到几道面试题,很基础,但不好回答(进入有分)

http://community.csdn.net/Expert/topic/3451/3451720.xml?temp=9.774417E-02

贴几个经典面试题,怎么做

http://community.csdn.net/Expert/topic/3378/3378595.xml?temp=.1972315

文件操作,读取一组数进行排序然后写入文件

http://community.csdn.net/Expert/topic/3356/3356378.xml?temp=.07922

一道面试题!在线恭候大家!交换两个数,不用第三块儿内存!请问怎么实现?

int a,intb

a ^= b ^= a  ^= b;

a+=b;

b=a-b;

a=a-b;

http://community.csdn.net/Expert/topic/3343/3343124.xml?temp=.1771509

[高分求教]C语言笔试或面试一般会问到什么问题?

http://community.csdn.net/Expert/topic/3218/3218202.xml?temp=.8798181

   有一个函数里面是大概是这样的。
    function(a*,b*){
       case:变量{
           default;
         while(........){
            case 1:   a++ = b++;
            case 2:  a++ = b++;
            case 3:   a++ = b++;
            case 4:  a++ = b++;
            case 5:  a++ = b++;
            case 6:  a++ = b++;
         }
       }
    }
问题是这个函数在做什么,看了半天,不明白为什么case 的分项会放到一个循环里。

http://community.csdn.net/Expert/topic/3313/3313398.xml?temp=5.338687E-02

1.给定下面的声明:
typedef struct
{
   int x;
   int y;
}Point;

Point x,y;
Point *a=&x, *b=&y;
解释下列各语名的含义:
a x=y;
b a=y;
c a=b;
d a=*b;
e *a=*b;


2.给定下列声明
  int  array[4][5][3];
把下列各个指针表达式转换为下标表达式

表达式                                                下标表达式
*array
*(array+2)
*(array+1)+4
*(*(array+1)+4)
*(*(*(array+3)+1)+2)
*(*(*array+1)+2)
*(**array+2)
**(*array+1)
***array

 

3.当你拨打长途电话时,电话公司所保存的信息包括你拨打电话的日期和时间.它还包括三个电话号码,
:你使用的那个电话,你呼叫的那个电话以及你付帐的那个电话.这些电话号码的每一个都由三个部分组成,
:区号,交换台和站号码.请为这些记帐信息编定一个结构声明

 

4.表达式(a)和(b)的求值过程有没有区别,如果有的话,区别在哪里?假定变量offset的
值是3.
int i[10];
int *p=&i[0];
int offset;
p+=offset;    (a)
p+=3;         (b

http://community.csdn.net/Expert/topic/3293/3293716.xml?temp=.7074396

一道面试题,请大虾们帮忙评测一下:难度系数、具体的实现算法,谢谢

http://community.csdn.net/Expert/topic/3274/3274011.xml?temp=.1997187

1. Project Assignment -- Bounded Buffer Producer Consumer

Implement the bounded buffer producer and consumer problem and write a
simulation on your implementation. The size of the bounded buffer
should be adjustable at initialization. You can assume that the type
of the item produced and consumed is of an integer type, and that
producers produce a sequence of integers. The simulation should
include multiple producers and multiple consumers sharing the same
bounded buffer. Run the simulation for 10000 iterations, in each of
which an item is produced or consumed.

2. Project Assignment -- The Shell

Write the UNIX shell. Even the simplest of real UNIX shells is
thousands of lines of C code. Don’t worry, you will be writing a
minuscule, super-simplified shell. Your shell shall accept lines of
input from standard input until EOF. Don’t worry about issuing a
prompt, command-line editing, etc. Each line is a command to be
executed. The shell should fork to create a new process in which to
run the command, exec the program, wait for and report the exit
status, and report the user and system time consumed by the
command. You may assume that each command is formatted as follows:

command {argument {argument...} }

The above optional arguments are whitespace-delimited, i.e. they are
separated by one or more tabs or spaces. This will simplify parsing. A
line that begins with the # character is a comment and should be
ignored.

http://community.csdn.net/Expert/topic/3224/3224699.xml?temp=.2033655

面试题:求用一段C或C++程序写求 f(x)=100! 的完整程序

http://community.csdn.net/Expert/topic/3209/3209258.xml?temp=.7044336

25匹马,每次捡5匹跑,几次可找出最快的5匹马?

http://community.csdn.net/Expert/topic/3203/3203144.xml?temp=.2724115

一个面试题 求 n! (n的阶层)需要考虑溢出

http://community.csdn.net/Expert/topic/3169/3169555.xml?temp=.9761927

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值