首页 > 代码库 > C++经典面试算法题

C++经典面试算法题

转自:http://blog.csdn.net/f_r_e_e_x/article/details/50770907

 1 //1.实现strcpy.    
 2 char* MyStrCpy( char *pDest, const char *pSrc )    
 3 {    
 4     if( nullptr == pDest || nullptr == pSrc )    
 5     {    
 6         return nullptr;    
 7     }    
 8     if( pDest == pSrc )    
 9     {    
10         return pDest;    
11     }    
12     char *pIter = pDest;    
13     while( ( *pIter++=*pSrc++ ) !=\0 );    
14     return pDest;    
15 }    

 

 1 //2.实现strcat.    
 2 char* MyStrCat( char *pDest, const char *pSrc )    
 3 {    
 4     if( nullptr == pDest || nullptr == pSrc )    
 5     {    
 6         return nullptr;    
 7     }    
 8   
 9     char *pIter = pDest + strlen( pDest );    
10     while( ( *pIter++=*pSrc++ ) != \0 );    
11     return pDest;    
12 }    

 

 1 //3.实现CString字符串类缺省四个方法    
 2 class MyCString    
 3 {    
 4 public:    
 5   
 6     MyCString( char *pData =http://www.mamicode.com/ nullptr )    
 7     {    
 8         if( nullptr == pData )    
 9         {    
10             mpData = http://www.mamicode.com/new char[ 1 ];    
11             assert( nullptr != mpData );    
12             *mpData = http://www.mamicode.com/\0;    
13         }    
14         else    
15         {    
16             mpData = http://www.mamicode.com/new char[ strlen( pData ) + 1 ];    
17             assert( nullptr != mpData );    
18             strcpy( mpData, pData );    
19         }    
20     }    
21   
22     MyCString( const MyCString &Other )    
23     {    
24         mpData = http://www.mamicode.com/new char[ strlen( Other.mpData ) + 1 ];    
25         assert( nullptr != mpData );    
26         strcpy( mpData, Other.mpData );    
27     }    
28   
29     ~MyCString()    
30     {    
31         if( nullptr != mpData )    
32         {    
33             delete [] mpData;    
34             mpData =http://www.mamicode.com/ nullptr;    
35         }    
36     }    
37   
38     const MyCString& operator =( const MyCString &Other )    
39     {    
40         if( this == &Other )    
41         {    
42             return *this;    
43         }    
44         delete [] mpData;    
45         mpData = http://www.mamicode.com/new char[ strlen( Other.mpData ) + 1 ];    
46         assert( nullptr != mpData );    
47         strcpy( mpData, Other.mpData );    
48         return *this;    
49     }    
50   
51 private:    
52   
53     char *mpData;    
54 };    

 

 1 //4.不使用第三个变量交换两个数的值    
 2 void SwapA( int &A, int &B )    
 3 {    
 4     if( A == B )    
 5     {    
 6         return;    
 7     }    
 8     A = A + B;    
 9     B = A - B;    
10     A = A - B;    
11 }    
12 void SwapB( unsigned int &A, unsigned int &B )    
13 {    
14     if( A == B )    
15     {    
16         return;    
17     }  
18     A = A ^ B;    
19     B = A ^ B;    
20     A = A ^ B;    
21 }    

 

 1 //5.C语言中字符串转数字的方法是什么( atoi ),请实现它    
 2 int Myatoi( const char *pString )    
 3 {    
 4     assert( nullptr != pString );    
 5     const int Len = strlen( pString );    
 6     int Value = http://www.mamicode.com/0;    
 7     int Times = 1;    
 8     for( int i = Len -1; i >= 0; --i, Times *= 10 )    
 9     {    
10         Value += ( pString[ i ] - 0 ) * Times;    
11     }    
12     return Value;    
13 }    

 

 1 //6.实现一个将字符串逆序的方法    
 2 char* MyInverted( char *pDest )    
 3 {    
 4     assert( nullptr != pDest );    
 5     const int Len = strlen( pDest );    
 6     char T = 0;    
 7     for( int i = 0; i < Len / 2; ++i )    
 8     {    
 9         T = pDest[ i ];    
10         pDest[ i ] = pDest[ Len - i - 1 ];    
11         pDest[ Len - i -1 ] = T;    
12     }    
13     return pDest;    
14 }    

 

 1 //7.实现一个将字符串中所有字母转换为大写的方法    
 2 char* MyUpper( char *pDest )    
 3 {    
 4     assert( nullptr != pDest );    
 5     for( char *i = pDest; *i != \0; ++i )    
 6     {    
 7         if( *i < a || *i > z )    
 8         {    
 9             continue;    
10         }    
11         *i -= a - A;    
12     }    
13     return pDest;    
14 }    

 

 1 //8.已知一个数组已经降序排序请用二分查字法找到其中的某个元素找到返回索引否则返回-1    
 2 int BinarySearch( int *pArray, int Count, int Value )    
 3 {    
 4     assert( nullptr != pArray );    
 5     int Left = 0;    
 6     int Right = Count -1;    
 7     int Mid = 0;    
 8     while( Left <= Right )    
 9     {    
10         Mid = ( Left + Right ) / 2;    
11         if( Value < pArray[ Mid ] )    
12         {    
13             Right = Mid - 1;    
14         }    
15         else if( Value > pArray[ Mid ] )    
16         {    
17             Left = Mid + 1;    
18         }    
19         else    
20         {    
21             return Mid;    
22         }    
23     }    
24     return -1;    
25 }    
26   
27 struct Node    
28 {    
29     Node *mpNext;    
30     int mData;    
31 };  

 

 1 //9.删除链表中值为Value的所有元素( [Head]->[node1]->[node2]->...[noden] )  
 2 void DeleteFromList( Node *pHead, int Value )  
 3 {  
 4     Node *pPrev = pHead;  
 5     Node *pNext = pHead->mpNext;  
 6     while( nullptr != pNext )  
 7     {  
 8         if( pNext->mData != Value )  
 9         {  
10             pPrev = pNext;  
11             pNext = pNext->mpNext;  
12         }  
13         else  
14         {  
15             pPrev->mpNext = pNext->mpNext;  
16             delete pNext;  
17             pNext = pPrev->mpNext;  
18         }  
19     }  
20 }    

 

 1 //10.在链表Index位置插入新的值为Value的元素    
 2 void InsertFromList( Node *pHead, int Index, int Value )    
 3 {  
 4     Node *pIter = pHead;  
 5     for( int i = 0; i < Index && nullptr != pIter; ++i, pIter = pIter->mpNext );  
 6     assert( nullptr != pIter );  
 7     Node *pNew = new Node;  
 8     pNew->mData =http://www.mamicode.com/ Value;  
 9     pNew->mpNext = pIter->mpNext;  
10     pIter->mpNext = pNew;  
11 }    

 

 1 //11.将链表逆序    
 2 Node* InvertedFromList( Node *pHead )    
 3 {    
 4     //A->B->C  
 5     Node *pPrev = pHead;            //A  
 6     Node *pNext = pHead->mpNext;        //B  
 7     Node *pNextNext = nullptr;        //C  
 8     while( nullptr != pNext )    
 9     {  
10         pNextNext = pNext->mpNext;    //C = B->C  
11         pNext->mpNext = pPrev;        //B->A  
12   
13         pPrev = pNext;                //A = B  
14         pNext = pNextNext;            //B = C  
15     }  
16     pHead->mpNext = nullptr;//C->B->A->null  
17     return pPrev;            //return C( new head )  
18 }    

 

 1 //12.判断X年X月X日是这年的第几天    
 2 int GetDay( int Year, int Month, int Day )  
 3 {    
 4     int MonthDays[ 13 ] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };    
 5   
 6     if( ( Year % 4 == 0 && Year % 100 != 0 ) || ( Year % 400 == 0 ) )    
 7     {    
 8         ++MonthDays[ 2 ];    
 9     }    
10   
11     int Days = 0;    
12     for( int i = 1; i < Month; ++i )    
13     {    
14         Days += MonthDays[ i ];    
15     }    
16     Days += Day;    
17   
18     return Days;    
19 }  

 

 1 //13.求斐波拉契数列第N项  
 2 int GetFibonacci1( int N )  
 3 {  
 4     if( 1 == N || 2 == N )  
 5     {  
 6         return 1;  
 7     }  
 8     if( 3 == N )  
 9     {  
10         return 2;  
11     }  
12     int A = 2;  
13     int B = 3;  
14     int C = 5;  
15     for( int i = 0; i < N - 4; ++i )  
16     {  
17         C = A + B;  
18         A = B;  
19         B = C;  
20     }  
21     return C;  
22 }  

 

1 //14.递归求斐波拉契数列数列第N项  
2 int GetFibonacci2( int N )  
3 {  
4     if( 1 == N || 2 == N )  
5     {  
6         return 1;  
7     }  
8     return GetFibonacci2( N - 1 ) + GetFibonacci2( N - 2 );  
9 }  

 

 1 //15.实现一个产生[N-M]区间数字的随机方法  
 2 int GetRandomRange( int N, int M )  
 3 {  
 4     if( N == M )  
 5     {  
 6         return N;  
 7     }  
 8     if( N > M )  
 9     {  
10         N = N + M;  
11         M = N - M;  
12         N = N - M;  
13     }  
14     return N + ( rand() % ( M - N + 1 ) );  
15 }  

 

1 //16.实现一个产生[0~1]之间的随机浮点数  
2 double GetRandomRange()  
3 {  
4     return rand() / static_cast< double >( RAND_MAX );  
5 }  

 

 1 //17.实现一个打印出1-1000之间的所有素数的方法  
 2 void PrintfPrime()  
 3 {  
 4     //1不是素数  
 5     //2是最小非奇数素数  
 6     //直接从3开始  
 7     printf( "2\n" );  
 8     bool b = false;  
 9     for( int i = 3; i <= 1000; ++i )  
10     {  
11         b = true;  
12         for( int j = 2; j <= i / 2; ++j )  
13         {  
14             if( i % j == 0 )  
15             {  
16                 b = false;  
17                 break;  
18             }  
19         }  
20         if( b )  
21         {  
22             printf( "%d\n", i );  
23         }  
24     }  
25 }  

 

 1 //18.已知Z = X + Y 其中 Z, X, Y 均为无符号int型 定义一个宏判断Z是否已经越界 2 3 #define IS_OVER_FLOW( Z, X, Y ) ( Z < ( ( X ) < ( Y ) ? ( Y ) : ( X ) ) )  

 

 

 1 //19.请用栈实现队列  
 2 int QueuePop( std::stack< int > &StackA )  
 3 {  
 4     std::stack< int > StackB;  
 5     while( false == StackA.empty() )  
 6     {  
 7         StackB.push( StackA.top() );  
 8         StackA.pop();  
 9     }  
10   
11     const int top = StackB.top();  
12     StackB.pop();  
13   
14     while( false == StackB.empty() )  
15     {  
16         StackA.push( StackB.top() );  
17         StackB.pop();  
18     }  
19     return top;  
20 }  

 

 1 //20.已知X班X成绩0-100分编写一个方法实现0-59打印不合格,60-69打印合格,70-79打印良好,80-100打印优秀  
 2 //不能使用if,:?,switch  
 3 void PrintScore( int Score )  
 4 {  
 5     assert( Score >= 0 && Score <= 100 );  
 6     const char *pString[] =  
 7     {   
 8         "不合格",  
 9         "不合格",  
10         "不合格",  
11         "不合格",  
12         "不合格",  
13         "不合格",  
14         "合格",  
15         "良好",  
16         "优秀",  
17         "优秀",  
18         "优秀",  
19     };  
20     printf( "%s\n", pString[ Score / 10 ] );  
21 }  

 

 1 //21.实现strncpy  
 2 char *Mystrncpy( char *pDest, const char *pSrc, int Count )  
 3 {  
 4     assert( NULL != pDest && NULL != pSrc );  
 5     if( pDest == pSrc )  
 6     {  
 7         return pDest;  
 8     }  
 9     if( Count <= 0 )  
10     {  
11         return pDest;  
12     }  
13     char *pStart = pDest;  
14     while( ( Count-- ) > 0 && ( *pStart++=*pSrc++ ) );  
15     *pStart = \0;  
16     return pDest;  
17 }   

 

 1 //22.C语言中数字转字符串的方法是什么?(itoa)请实现他  
 2 char* Myitoa( char *pDest, int val, int radix )  
 3 {  
 4     assert( NULL != pDest );  
 5     assert( radix > 1 );  
 6     const bool IsMinu = val < 0;  
 7     char buffer[ 16 ] = {};  
 8     int count = 0;  
 9   
10     do  
11     {  
12         buffer[ count++ ] = abs(val) % radix;  
13         val /= radix;  
14     }  
15     while( val );  
16   
17   
18     if( IsMinu )  
19     {  
20         pDest[ 0 ] = -;  
21         for( int i = 0; i < count; ++i )  
22         {  
23             pDest[ i + 1 ] = 0 + buffer[ count - i - 1 ];  
24         }  
25         pDest[ count + 1 ] = \0;  
26     }  
27     else  
28     {  
29         for( int i = 0; i < count; ++i )  
30         {  
31             pDest[ i ] = 0 + buffer[ count - i - 1 ];  
32         }  
33         pDest[ count ] = \0;  
34     }  
35     return pDest;  
36 }  

 

 1 //23.如何判断链表是否有环  
 2 bool IsLoop( Node *pHead )  
 3 {  
 4     //[H->A->B->C->A]  
 5     assert( NULL != pHead );  
 6     Node *pNext = pHead->mpNext;  
 7     Node *pNextNext = pHead->mpNext;  
 8     while( NULL != pNext && NULL != pNextNext->mpNext )  
 9     {  
10         pNext = pNext->mpNext;//[ B、C、A ]  
11         pNextNext = pNextNext->mpNext->mpNext;//[C、B、A]  
12         if( pNext == pNextNext )  
13         {  
14             return true;  
15         }  
16     }  
17     return false;  
18 }  

 

 1 //24.统计出一个字符串每种字母出现的次数要求时间复杂度为O(n)  
 2 void CountLetter( const char *pSrc )  
 3 {  
 4     int count[ 256 ] = {};  
 5     for( ; *pSrc !=\0; ++pSrc )  
 6     {  
 7         const char &c = *pSrc;  
 8         if( ( c < A || c > z) && ( c < a || c > z ) )  
 9         {  
10             continue;  
11         }  
12         ++count[ c ];  
13     }  
14 }   

 

 1 //25.选择排序的思想是什么?( 每次找到最大或最小的值放在数组的低位上 )请实现它  
 2 void SelectSort( int *pArray, int count )  
 3 {  
 4     for( int i = 0; i < count; ++i )  
 5     {  
 6         //默认低位元素最小  
 7         int MinValue =http://www.mamicode.com/ pArray[ i ];  
 8         //默认保存低位元素的索引  
 9         int MinIndex = i;  
10         //除开第一个元素找是否还有比它还小的元素( 升序 )  
11         for( int j = i + 1; j < count; ++j )  
12         {  
13             //发现找到比它还小的元素重新赋值和保存索引  
14             if( pArray[ j ] < MinValue )  
15             {  
16                 MinValue =http://www.mamicode.com/ pArray[ j ];  
17                 MinIndex = j;  
18             }  
19         }  
20         //将找到最小元素放在数组低位上面  
21         const int Temp = pArray[ i ];  
22         pArray[ i ] = MinValue;  
23         pArray[ MinIndex ] = Temp;  
24     }  
25 }  

 

 1 //26.冒泡排序的思想是什么?(升序排序中越小的数往低位走,越大的数往高位走,每次与相邻元素比较导致的特点)请实现它  
 2 void BubbleSort( int *pArray, int count )  
 3 {  
 4     //eg.[6][8][8][0][9][1]  
 5     //i = 0,j < 5    [6][8][0][8][1][9]  
 6     //i = 1,j < 4    [6][0][8][1][8][9]  
 7     //i = 2,j < 3    [0][6][1][8][8][9]  
 8     //i = 3,j < 2    [0][1][6][8][8][9]  
 9   
10     //到此为止已经排序OK了  
11     //i = 4,j < 1    [0][1][6][8][8][9]  
12     //i = 5,j < 0    [0][1][6][8][8][9]  
13     for( int i = 0; i < count; ++i )  
14     {  
15         for( int j = 0; j < count - i - 1; ++j )  
16         {  
17             if( pArray[ j ] > pArray[ j + 1 ] )  
18             {  
19                 const int Temp = pArray[ j ];  
20                 pArray[ j ] = pArray[ j + 1 ];  
21                 pArray[ j + 1 ] = Temp;  
22             }  
23         }  
24     }  
25 }  

 

 1 //27.已知两个数组有序实现一个方法将他们合并后任然有序  
 2 void MergeSort( int *pMerge, int *p1, int p1len, int *p2, int p2len )  
 3 {  
 4     assert( nullptr != pMerge && nullptr != p1 && nullptr != p2 );  
 5     int i = 0;  
 6     int j = 0;  
 7     int k = 0;  
 8     while( i < p1len && j < p2len )  
 9     {  
10         if( p1[ i ] < p2[ j ] )  
11         {  
12             pMerge[ k ] = p1[ i ];  
13             ++k;  
14             ++i;  
15         }  
16         else  
17         {  
18             pMerge[ k ] = p2[ j ];  
19             ++k;  
20             ++j;  
21         }  
22     }  
23     while( i < p1len )  
24     {  
25         pMerge[ k ] = p1[ i ];  
26         ++k;  
27         ++i;  
28     }  
29     while( j < p2len )  
30     {  
31         pMerge[ k ] = p2[ j ];  
32         ++k;  
33         ++j;  
34     }  
35 }  

 

 1 //28.实现一个算法找到数组中第二大的数  
 2 int FindSec( int *p, int len )  
 3 {  
 4     assert( nullptr != p );  
 5     int maxv = p[ 0 ];  
 6     int secv = p[ 0 ];  
 7     for( int i = 1; i < len; ++i )  
 8     {  
 9         if( maxv < p[ i ] )  
10         {  
11             secv = maxv;  
12             maxv = p[ i ];  
13         }  
14     }  
15     return secv;  
16 }  

 

C++经典面试算法题