經典問題9:c/c++ 程序設計 ---基本數據處理問題

-------------------------------------------------------------------
經典問題9:c/c++ 程序設計 ---基本數據處理問題
-------------------------------------------------------------------
=================================
    (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    }
=================================

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章