2014年 去哪儿网 开发笔试题

1.   将绝对路径转换成相对路径。例如, input: /home/news/../tmp/game/../; ouptut: /home/tmp/

  思路:利用栈的思想。每次遇到".."时,将退栈至上一个'/'位置。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *convert_path_opt( const char *path )
{
    char *result    = NULL;
    int top         = 0;
    int path_len    = 0,
        index       = 0,
        point_cnt   = 0;

    /**< check */
    if ( NULL == path )
    {
        fprintf( stderr, "convert_path_opt: invalid argument.\n" );
        return NULL;
    }

    /**< allocate memory */
    path_len = strlen( path );
    result = (char *)malloc( path_len * sizeof(char) );
    if ( NULL == result )
    {
        fprintf( stderr, "convert_path_opt: failed to malloc.\n" );
        return NULL;
    }

    /**< convert */
    while ( index < path_len )
    {
        /**< copy characters before point. */
        while ( index < path_len && path[index] != '.' )
        {
            result[top++] = path[index++];
        }

        /**< counter point. */
        for ( point_cnt = 0;
                index < path_len && path[index] == '.';
                ++point_cnt, ++index );

        if ( point_cnt == 2 )
        {
            --top;
            while ( --top > 0 && result[top] != '/' );
            ++top;
        }

        ++index;
    }

    result[top] = '\0';

    return result;
}

int main()
{
    char *path = "/home/news/./../tmp/game/../";
    char *result = NULL;

    result = convert_path_opt( path );
    printf( "\nResult is %s.\n", result );

    return 0;
}

2.  比较两个字符串的不同,返回不同点的位置(相对第二字符串而言)

#include <stdio.h>

char *compare_strings( const char *plhs,
                       const char *prhs )
{
    int lhs_index   = 0,
        rhs_index   = 0;

    /**< check argument. */
    if ( NULL == plhs || NULL == prhs )
    {
        fprintf( stderr, "copmare_strings:invalid argument.\n" );
        return NULL;
    }

    /**< compare */
    while ( plhs[lhs_index] != '\0'
           && prhs[rhs_index] != '\0' )
    {
        if ( plhs[lhs_index] != prhs[rhs_index] )
            break;
        ++lhs_index;
        ++rhs_index;
    }

    return prhs + rhs_index;
}

int main()
{
    char *plhs = "hello, world.";
    char *prhs = "hello, world.";
    char *pdiff = NULL;

    pdiff = compare_strings( plhs, prhs );
    printf( "\nDifference is %s.\n", pdiff );

    return 0;
}

3. 给定一个100亿的整数文件,输出前100个最小值。

  思路:大数据+top k问题。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/**< max_heap */
struct max_heap
{
    int current_index;
    int capacity;
    int *elem;
};
typedef struct max_heap max_heap;

/**< allocate max_heap */
max_heap *alloc_max_heap( const int cap )
{
    max_heap *new_heap = NULL;

    /**< check parameter */
    if ( cap <= 0 )
    {
        fprintf( stderr, "alloc_max_heap: invalid parameter.\n" );
        return NULL;
    }

    /**< allocate memory */
    new_heap = (max_heap *) malloc( sizeof(max_heap) );
    if ( NULL == new_heap )
    {
        fprintf( stderr, "alloc_max_heap: failed to malloc.\n" );
        return NULL;
    }

    new_heap->elem = (int *) calloc( cap, sizeof(int) );
    if ( NULL == new_heap->elem )
    {
        fprintf( stderr, "alloc_max_heap: failed to calloc.\n" );
        free( new_heap ), new_heap = NULL;  /**< free allocated memory. */
        return NULL;
    }

    /**< set other parameter. */
    new_heap->capacity = cap;
    new_heap->current_index = 0;

    return new_heap;
}

/**< destroy max_heap */
void destroy_max_heap( max_heap **heap )
{
    if ( NULL != heap )
    {
        if ( (*heap)->elem )
        {
            free( (*heap)->elem );
            (*heap)->elem = NULL;
        }

        free( *heap );
        *heap = NULL;
    }
}

/**< swap elements */
void swap_int( int *number, int lhs, int rhs )
{
    int tmp = number[lhs];
    number[lhs] = number[rhs];
    number[rhs] = tmp;
}

/**< insert value */
void push_value( max_heap *heap, const int value )
{
    int left_child      = 0,
        right_child     = 0,
        parent_index    = 0;

    /**< check argument */
    if ( NULL == heap )
    {
        fprintf( stderr, "push_value: invalid parameter.\n" );
        return;
    }
    if ( heap->current_index == heap->capacity )
    {
        fprintf( stderr, "push_value: this max_heap is full.\n" );
        return;
    }

    /**< push element */
    parent_index = heap->current_index;
    heap->elem[heap->current_index++] = value;

    while ( parent_index >= 0 )
    {
        left_child = 2 * parent_index + 1;
        right_child= 2 * parent_index + 2;

        if ( left_child < heap->current_index
            && heap->elem[left_child] > heap->elem[parent_index] )
        {
            swap_int( heap->elem, left_child, parent_index );
        }

        if ( right_child < heap->current_index
            && heap->elem[right_child] > heap->elem[parent_index] )
        {
            swap_int( heap->elem, right_child, parent_index );
        }

        --parent_index;
    }
}

/**< pop element */
int pop_value( max_heap *heap )
{
    int ret             = 0,
        left_child      = 0,
        right_child     = 0,
        parent_index    = 0,
        swap_index      = 0;

    /**< check argument. */
    if ( NULL == heap )
    {
        fprintf( stderr, "pop_value: invalid parameter.\n" );
        return -1;
    }
    if ( heap->current_index == 0 )
    {
        fprintf( stderr, "pop_value: this max_heap is empty.\n" );
        return -1;
    }

    /**< pop element */
    --heap->current_index;
    swap_int( heap->elem, 0, heap->current_index );
    ret = heap->elem[heap->current_index];
    parent_index = 0;
    while ( parent_index < heap->current_index )
    {
        left_child = 2 * parent_index + 1;
        right_child= 2 * parent_index + 2;

        /**< looking for maximum between left and right. */
        if ( left_child < heap->current_index
             && right_child < heap->current_index )
        {
            swap_index = heap->elem[left_child] > heap->elem[right_child] ?
                            left_child : right_child;
        }
        else if ( left_child < heap->current_index )
        {
            swap_index = left_child;
        }
        else if ( right_child < heap->current_index )
        {
            swap_index = right_child;
        }

        /**< if heap->elem[swap_index] is greater than heap->elem[parent], swap each other. */
        if ( heap->elem[swap_index] > heap->elem[parent_index] )
        {
            swap_int( heap->elem, swap_index, parent_index );
            parent_index = swap_index;
        }
        else
        {
            break;
        }
    }

    return ret;
}

/**< test max_heap */
void test_max_heap()
{
    int number[] = { 79, 81, 97, 21, 88, 15, 1, 61, 28, 68 };
    int size_i = sizeof( number ) / sizeof( int );
    int index = 0;
    max_heap *heap = NULL;

    heap = alloc_max_heap( size_i);
    for ( index = 0; index < size_i; ++index )
    {
        push_value( heap, number[index] );
    }

    printf( "\nthe element in max_heap is \n" );
    while ( heap->current_index )
    {
        index = pop_value( heap );
        printf( " %d", index );
    }
    printf( "\n" );

    destroy_max_heap( &heap );
}

/**< generating data */
void generating_data( const char *file_name, const int size_i, const int range )
{
    FILE *output_file = NULL;
    int index = 0,
        data = 0;

    /**< check parameter */
    if ( NULL == file_name || size_i <= 0 || range <= 0 )
    {
        fprintf( stderr, "generating_data: invalid parameter.\n" );
        return;
    }

    /**< open file. */
    output_file = fopen( file_name, "w" );
    if ( NULL == output_file )
    {
        fprintf( stderr, "generating_data: failed to open file %s.\n", file_name );
        return;
    }

    /**< write data into file. */
    srand( (unsigned) time(0) );
    while ( index < size_i )
    {
        data = rand() % range;
        fprintf( output_file, "%d\n", data );
        ++index;
    }

    fclose( output_file );
}

/**< output min k elements */
void output_min_k_elements( const char *file_name, const int k )
{
    max_heap *heap = NULL;
    FILE *input_file = NULL;
    int data = 0,
        index = k;

    /**< check argument. */
    if ( NULL == file_name || k <= 0 )
    {
        fprintf( stderr, "output_min_k_elements: invalid parameter.\n" );
        return;
    }

    /**< open file */
    input_file = fopen( file_name, "r" );
    if ( NULL == input_file )
    {
        fprintf( stderr, "output_min_k_elements: failed to open file %s.\n", file_name );
        return;
    }

    heap = alloc_max_heap( k );
    /**< fill heap */
    while ( index-- > 0 && fscanf( input_file, "%d", &data) != EOF )
    {
        push_value( heap, data );
    }

    while ( fscanf( input_file, "%d", &data ) != EOF )
    {
        if ( heap->current_index == k
            && data < heap->elem[0] )
        {
            pop_value( heap );
            push_value( heap, data );
        }
    }

    /**< output data */
    while ( heap->current_index )
    {
        data = pop_value( heap );
        printf( " %d", data );
    }

    /**< free memory and close file. */
    destroy_max_heap( &heap );
    fclose( input_file );
}

/**< test */
void test_func( void )
{
    char *file_name = "e:\\test.txt";
    int k = 10;
    int size_i = 100000;
    int range = 100000000;

    /**< generating data */
    generating_data( file_name, size_i, range );
    output_min_k_elements( file_name, k );
}

int main()
{
    test_func();

    return 0;
}

4. 通过随机函数生成随机数填充一个二维数组,随机数被4取余,其中0表示red,1表示blue,2表示green,3表示black。根据五子棋的规则,5个连续在一起的输出。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void gobang_problem( int colors[][10], int size_i )
{
    int i, j, k, p,
        colors_cnt = 0;

    /**< check argument. */
    if ( NULL == colors || size_i <= 0 )
    {
        fprintf( stderr, "gobang_problem: invalid argument.\n" );
        return;
    }

    /**< compute */
    for ( i = 0; i < size_i; ++i )
    {
        for ( j = 0; j < size_i; ++j )
        {
            /**< 纵向 */
            for ( colors_cnt = 0, k = j; k < size_i; ++k )
            {
                if ( colors[i][j] == colors[i][k] )
                {
                    colors_cnt++;
                    /**< if found, output. */
                    if ( colors_cnt == 5 )
                    {
                        while ( k >= j )
                        {
                            printf( " %d %d,", i, k-- );
                        }

                        return;
                    }
                }
                else
                {
                    break; /**< if not same, do not need to continue. */
                }
            }

            /**< 横向 */
            for ( colors_cnt = 0, k = i; k < size_i; ++k )
            {
                if ( colors[i][j] == colors[k][j] )
                {
                    colors_cnt++;
                    if ( colors_cnt == 5 )
                    {
                        while ( k >= i )
                        {
                            printf( " %d %d,", k--, j );
                        }

                        return;
                    }

                }
                else
                {
                    break;
                }
            }

            /**< 向下斜向 */
            for ( colors_cnt = 0, k = i, p = j;
                    k < size_i && p < size_i; ++k, ++p )
            {
                if ( colors[i][j] == colors[k][p] )
                {
                    colors_cnt++;
                    if ( colors_cnt == 5 )
                    {
                        while ( k >= i && p >= j )
                        {
                            printf( " %d %d,", k--, p-- );
                        }

                        return;
                    }

                } // end if
                else
                {
                    break;
                }
            }// end for
        } // end for
    } // end for


    /**< 向上斜向 */
    for ( i = size_i - 1; i >= 0; --i )
    {
        for ( j = 0; j < size_i; ++j )
        {
            for ( colors_cnt = 0, k = i, p = j;
                    k >= 0 && p < size_i;
                    --k, ++p )
            {
                if ( colors[i][j] == colors[k][p] )
                {
                    colors_cnt++;
                    if ( colors_cnt == 5 )
                    {
                        while ( k <= i && p >= j )
                        {
                            printf( " %d %d,", k++, p-- );
                        }

                        return;
                    }

                }
                else
                {
                    break;
                }
            } // end for
        }
    }
}

void generate_chessboard( int chessboard[][10], int size_i )
{
    int i, j;

    /**< check argument */
    if ( NULL == chessboard || size_i <= 0 )
    {
        fprintf( stderr, "generate_chessboard: invalid argument.\n" );
        return;
    }

    /**< generating */
    srand( (unsigned) time(0) );
    for ( i = 0; i < size_i; ++i )
    {
        for ( j = 0; j < size_i; ++j )
        {
            chessboard[i][j] = rand() % 4;
        }
    }

    /**< output chessboard */
    printf( "\nThis color in chessboard is \n" );
    for ( i = 0; i < size_i; ++i )
    {
        for ( j = 0; j < size_i; ++j )
        {
            printf( " %d", chessboard[i][j] );
        }
        printf( "\n" );
    }
}

int main()
{
    int chessboard[10][10];
    int size_i = 10;

    generate_chessboard( chessboard, size_i );
    printf( "\nFive colors in low:\n" );
    gobang_problem( chessboard, size_i );

    return 0;
}

个人资料
onemore
等级:8
文章:133篇
访问:11.8w
排名: 4
上一篇: 2015去哪儿网产品经理-长春
下一篇:去哪儿网2013校园招聘产品经理笔试题目
猜你感兴趣的圈子:
去哪儿网笔试面试圈
标签: heap、index、colors、elem、child、面试题
隐藏