baidu2007校園招聘筆試題(第二題)

百度筆試題:

ip地址存在文件中,例如10.0.0.1 10.0.0.10,一共有n行,文件小於2k,表示爲ip地址的一個範圍(也就是說前一個小,後一個大)

例如:

10.0.0.1 10.0.0.10

10.0.0.4 10.0.0.8

10.0.0.6 10.0.0.15

10.0.0.23 10.0.0.123

輸出爲: 10.0.0.1 10.0.0.123

 

/////////////////////////////////////////////////////
////            baidu Corp.                /////
////    2007.10.22 Paper Test in Nanjing.    //////
/////////////////////////////////////////////////////


#include 
<iostream>
#include 
<fstream>
#include 
<sstream>
#include 
<ctime>
#include 
<vector>

using namespace std;

//we can also definie a class, then overload operator '>' and  '<' to solve this problem.
//And maybe use this method, have a kind of oo thinking.
typedef struct tagip
{
    
int a;
    
int b;
    
int c;
    
int d;
}
_ip;

/*
*func: open file
*parameter1: file stream [in]
*parameter2: file name
*return: file stream [out]
*/

fstream 
& open_file( fstream & s, const string & file )
{
    s.close();
    s.clear();
    s.open( file.c_str(), fstream::
in );
    
return s;
}


/*
*func: check the first num is less than the second one
*parameter1: the first nums a[0].a[1].a[2].a[3]
*parameter2: get the value required to check
*parameter3: the num of checking
*return ...
*/

int check_num( int a[], int& value, int i )
{
    
if( a[i] > value ) return 1;
    
if( a[i] == value ) return 0;
    
return -1;
}


/*
*func: produce ip.txt
*similar as:
*10.0.0.0 10.0.0.1
*problem: cannot control the first number is less than the second one
*/

void produce_ip_file( const string& filename )
{
    fstream file;
    file.open( filename.c_str(), fstream::
out );
    
    srand( ( unsigned 
int )time( NULL ) );    //seed
    int i, j, value;
    
int a[4];    //a[0].a[1].a[2].a[3]
    int index;
    
string str_ip;
    
    
    
for( i = 0; i < 40; i++ )
    
{
        ostringstream stream_string;    
//format input value
        index = 0;
        
for( j = 0; j < 8; j++ )
        
{
            
if( j < 4 )
            
{
                a[j] 
= rand() % 255;
                stream_string 
<< a[j];
            }

            
else
            
{
                
if( index == 0 )
                
{
                    
do
                    
{
                        value 
= rand() % 255;
                    }
while( ( index = check_num( a, value, j-4 ) ) == 1 );
                    stream_string  
<< value;
                }

                
else if( index == -1 )
                
{
                    stream_string 
<< rand() % 255;
                }

            }

            
            
if( j != 3 && j != 7)
            
{
                stream_string 
<< ".";
            }

            
else if( j != 7 )
            
{
                stream_string 
<< " ";
            }

        }

        stream_string 
<< " ";
        file 
<< stream_string.str();
    }

    
    file.close();
    file.clear();
}


/*
*func: read ip from file, and decompose these data to structure
*parameter1: filename
*parameter2: this first ip-address is decomposed into struct
*parameter3: this second ip-address is decomposed into struct
*/

void read_file( const string& filename, vector< _ip * >& vec_ip1, vector< _ip* >& vec_ip2 )
{
    fstream file;
    
if!open_file( file, filename ) )
    
{
        cout 
<< "cannot open " << filename << endl;
        
return ;        
    }

    
    
int a1,b1,c1,d1;
    
int a2,b2,c2,d2;
    
string line;
    
char c;
    
while( getline( file, line ) )
    
{
        istringstream stream(line);
        stream 
>> a1 >> c
               
>> b1 >> c
               
>> c1 >> c
               
>> d1
               
>> a2 >> c
               
>> b2 >> c
               
>> c2 >> c
               
>> d2;
        _ip 
* _p1 = new _ip;
        _p1 
-> a = a1;
        _p1 
-> b = b1;
        _p1 
-> c = c1;
        _p1 
-> d = d1;
        vec_ip1.push_back( _p1 );
        _ip 
* _p2 = new _ip;
        _p2 
-> a = a2;
        _p2 
-> b = b2;
        _p2 
-> c = c2;
        _p2 
-> d = d2;
        vec_ip2.push_back( _p2 );
    }

}


/*
*func: get the minimun of the first ip-address and the maximun of the second ip-address --range
*parameter1: this first ip-address is decomposed into struct
*parameter2: this second ip-address is decomposed into struct
*parameter3: the index of minimun
*parameter4: the index of maximun
*/

void get_range( vector< _ip * >& vec_ip1, vector< _ip* >& vec_ip2, int& min, int& max )
{
    min 
= 0;
    max 
= 0;
    
int i = 1;
    
    vector
< _ip* >::const_iterator iter = vec_ip1.begin();
    _ip 
* _min = *iter;
    
++iter;
    
while( iter != vec_ip1.end() )
    
{
        
if( _min -> a > (*iter) -> a  )
        
{
            _min 
= *iter;
            min 
= i;
        }

        
else if( _min -> a == (*iter) -> a )
        
{
            
if( _min -> b > (*iter) -> b )
            
{
                _min 
= *iter;
                min 
= i;
            }

            
else if( _min -> b == (*iter) -> b )
            
{
                
if( _min -> c > (*iter) -> c )
                
{
                    _min 
= *iter;
                    min 
= i;
                }

                
else if( _min -> c == (*iter) -> c )
                
{
                    
if( _min -> d > (*iter) -> d )
                    
{
                        _min 
= *iter;
                        min 
= i;
                    }

                }
                    
            }

        }

        
++i;
        
++iter;
    }

    
    iter 
= vec_ip2.begin();
    _ip 
* _max = *iter;
    
++iter;
    i 
= 1;
    
while( iter != vec_ip2.end() )
    
{
        
if( _max -> a < (*iter) -> a  )
        
{
            _max 
= *iter;
            max 
= i;
        }

        
else if( _max -> a == (*iter) -> a )
        
{
            
if( _max -> b < (*iter) -> b )
            
{
                _max 
= *iter;
                max 
= i;
            }

            
else if( _max -> b == (*iter) -> b )
            
{
                
if( _max -> c < (*iter) -> c )
                
{
                    _max 
= *iter;
                    max 
= i;
                }

                
else if( _max -> c == (*iter) -> c )
                
{
                    
if( _max -> d < (*iter) -> d )
                    
{
                        _max 
= *iter;
                        max 
= i;
                    }

                }
                    
            }

        }

        
++i;
        
++iter;        
    }
    
}


int main( int argc, char **argv )
{
    
const string filename("ip.txt");
    produce_ip_file( filename );
    
    vector
< _ip * > vec_ip1;
    vector
< _ip * > vec_ip2;
    
    read_file( filename, vec_ip1, vec_ip2 );
    
    
int min, max;
    get_range( vec_ip1, vec_ip2, min, max );
    
    cout 
<< vec_ip1[min]-><< "." << vec_ip1[min]-><< "." << vec_ip1[min]-><< "." <<vec_ip1[min]-><< " "
         
<< vec_ip2[max]-><< "." << vec_ip2[max]-><< "." << vec_ip2[max]-><< "." <<vec_ip2[max]-><< endl;
         
    vector
< _ip* >::iterator iter = vec_ip1.begin();
    
while( iter != vec_ip1.end() )
    
{
        delete 
*iter;
        
++iter;
    }

    vec_ip1.clear();
    
    iter 
= vec_ip2.begin();
    
while( iter != vec_ip2.end() )
    
{
        delete 
*iter;
        
++iter;
    }

    vec_ip2.clear();    

    system(
"Pause");
    
return 0;
}

 

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