您的位置:首页 > 编程语言 > C语言/C++

经典问题9:c/c++ 程序设计 ---基本数据处理问题

2010-09-28 08:45 302 查看
(1)面试题 : There are two int variables:a and b,don't use “if”,”?:”

“switch”or other judgement statement,find out the biggest one of the two numbers.

答案:

方案一:

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

方案二:

BIG(a,b)((((INT32)(b))-((INT32)(a)))>>(sizeof(INT32)*8-1)&0x1)

=====================================

(2)面试题:如何将a,b的值进行交换,并且不使用任何中间变量?

答案:

a = a^b

b = a^b

a = a^b

=====================================

(3)面试题:评价一下c与c++的各自特点。

答案:

c是一个结构化语言,重点在于算法和数据结构。c语言的设计首要考虑的是如何通过一个过程,

对输入(或者环境条件)进行运算处理得出输出(或实现过程(事务)控制)。

c++,首要考虑的是如何构造一个对象模型,让这个模型能够默契与之对应的问题域,这样就可以通过获取对象的状态信息得到输出或实现过程(事务)控制。

对于大规模数值运算,C/C++和java/.NET之间没有明显的性能差异。

=================================

(3)面试题:如何打印出当前源文件的文件名以及源文件的当前行号?

答案:

通常使用的就是__FILE__, __LINE__,在调试函数中利用“%s","%ld",打印就好了。

=================================

(4)面试题:main主函数执行完毕后,是否可能会再执行一段代码,给出说明?

答案:

会执行另一些代码,进行处理工作。 如果你需要加入一段在main退出后执行的代码,可以使用atexit()函数,注册一个函数;

1 #include <stdlib.h>

2 #include <stdlib.h>

3 #include <stdio.h>

4

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

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

7

8 int main( void )

9 {

10 atexit( fn1 );

11 atexit( fn2 );

12 atexit( fn3 );

13 atexit( fn4 );

14 printf( "This is executed first./n" );

15 return 0;

16 }

17

18 void fn1()

19 {

20 printf("next./n");

21 }

22

23 void fn2()

24 {

25 printf("executed ");

26 }

27

28 void fn3()

29 {

30 printf("is ");

31 }

32

33 void fn4()

34 {

35 printf("This ");

36 }

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

结果:$ ./a.out

This is executed first.

This is executed next.

=================================

(5)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒;

程序:

1 #include <iostream>

2 #include <string>

3

4 using namespace std;

5

6 void ResetTheTime(int *year,int *month,int *date,int *hour,int *minute,int *second)

7 {

8 int dayofMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

9

10 if
(*year<0||*month<1||*month>12||*date<1||*date>31||*hour<0||*hour>23||*minute<0||*minute>59||*second<0||

*second>60)

11 {

12 return;

13 }

14

15 if (*year%400==0||*year%100!=0&&*year%4==0)

16 {

17 dayofMonth[1] = 29;

18 }

19 *second +=1;

20 if (*second>=60)

21 {

22 *second = 0;

23 *minute +=1;

24 if (*minute>=60)

25 {

26 *minute = 0;

27 *hour += 1;

28 if (*hour>=24)

29 {

30 *hour = 0;

31 *date +=1;

32 if (*date>=dayofMonth[*month-1])

33 {

34 *date = 1;

35 *month +=1;

36 if (*month>=12)

37 {

38 *month = 1;

39 *year +=1;

40 }

41 }

42 }

43 }

44 }

45 cout<<*year<<' '<<*month<<'
'<<*date<<' '<<*hour<<'
'<<*minute<<' '<<*second<<endl;

46 return;

47 }

48

49 int main()

50 {

51 int y1 = 2004;int m1 = 2;int d1 = 28;int h1 = 23;int mm = 59;int sec = 59;

52

53 ResetTheTime(&y1,&m1,&d1,&h1,&mm,&sec);

54 return 0;

55 }

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

结果:

$ ./a.out

2004 3 1 0 0 0

=================================

(5)一个五位数字ABCDE*4=EDCBA,这五个数字不重复,请编程求出来这个数字是多少?

程序:

1 #include <stdio.h>

2

3 int calc ()

4 {

5 int i;

6 for (i=10001; i<100000; i++)

7 {

8 int right = 0;

9 int left = i;

10 while ( left != 0 ) /*求右边的值*/

11 {

12 right = right * 10 + left % 10;

13 left /= 10;

14 }

15

16 if ( (i << 2) == right )

17 {

18 return i;

19 }

20 }

21

22 return -1;

23 }

24

25 //不用for,while,if,switch语句

26 //递归的算法:

27

28 int result (int i)

29 {

30 int a,b,c,d,e;

31 a = i / 10000;

32 b = ( i / 1000 ) % 10;

33 c = ( i / 100 ) % 10;

34 d = ( i / 10 ) % 10;

35 e = i % 10;

36

37 int right = e * 10000 + d * 1000 + c * 100 + b * 10 + a;

38 int left = i * 4;

39

/*如果相等就返回当前i值,说明找到;如果不相等,就递归找下一个*/

41 return ( ((right==left && a!=b && a!=c
&& a!=d && a!=e && b!=c && b!=d
&& b!=e && c!=d && c!=e && d!=e
&&

i>10000 ) || i <= 10000) ? i : result(i-1) );

42 }

43

44 int main(void)

45 {

46 int i = 99999;

47 printf("递归 :the result is : %d/n", result(i));

48 printf("非递归:the result is : %d/n", calc());

49 return 0;

50 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: