分享

美团网 笔试 2014

 看风景D人 2014-09-15

美团网研发工程师笔试题

 

 

1. 有一个随机数发生器,以概率P产生0,概率(1-P)产生1,请问能否利用这个随机数发生器,构造出新的发生器,以1/2的概率产生01。请写明结论及推理过程。

 

 

2. 一个栈的入栈序列是A,B,C,D,E,则栈的不可能的输出序列是( )

A. EDCBA;  B.  DECBA;  C.DCEAB  D,ABCDE

 

3. 4个足球队打小组单循环,计分方式:胜3分平1分负0分,如果计分相同,则净胜球多的队伍排名靠前,如果净胜球还一样,则进球多的球队排名靠前。小组前两名出线。问可能出线的最低分数是多少。请说明推理过程。

备注:单循环赛是指所有参加比赛的队两两之间都比赛一次,最后按各队在全部比赛中的积分,得失分率排列名次。

4. 从11000000的所有自然数,数字“1”一共出现了多少次?例:自然数101中,数字“1”出现了2次,自然数1011中,数字“1”出现了3次,请写明计算过程及结果

 

5. 以下代码是把一个字符串倒序,如“abcd”倒序后变为“dcba”。请找出下面代码中的所有错误,直接在代码的右侧空白处修改。

 

#include"string.h"

main()

{

char*src="hello,world";

char*dest=NULL;

int len = strlen(src);

dest = (char*)malloc(len);

char* d = dest;

char* s = src[len];

while(len--!=0)

d++ = s --;

printf("%s",dest);

return 0;

}

6. 以下代码功能:找出一个有序(字典序)字符串数组arr种值等于字符串v的元素的符号,如果有多个元素满足这个条件,则返回其中序号最大的。请找出下面代码中所有错误,直接在代码右侧空白处修改

 

Int bisearch(char**arr, int b, int e, char*v){

Int minIndex = b, maxIndex = e, midIndex;

while(minIndex<maxIndex){

midIndex=(minIndex+maxIndex)/2;

if(strcmp(arr[midIndx],v<=0)){

minIndex = midIndex;

}else{

maxIndex=minIndex;

}

}

if(!strcmp(arr[maxIndex],v)){

return maxIndex;

}else{

return -1;

}

 

}

 

7. 字符串ABCD,可以由字符串BCDA或者CDAB通过循环移位而得到。请编程实现以下

检测:字符串S1是否可以由字符串S2通过循环移位而得到。

语言不限(推荐C/C++,不推荐写伪码)

 

 

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


2014美团网笔试题目

   

1、一堆硬币,一个机器人,如果是反的就翻正,如果是正的就抛掷一次,无穷多次后,求正反的比例

 

解答:是不是题目不完整啊,我算的是31

 

 

2、一个汽车公司的产品,甲厂占40%,乙厂占60%,甲的次品率是1%,乙的次品率是2%,现在抽出一件汽车时次品,问是甲生产的可能性

 

解答:典型的贝叶斯公式,p(|废品) = p(甲 && 废品) / p(废品) = 0.4 × 0.01) /0.4 × 0.01 + 0.6 × 0.02) = 0.25

 

 

3k链表翻转。给出一个链表和一个数k,比如链表123456k=2,则翻转后214365,若k=3,翻转后321654,若k=4,翻转后432156,用程序实现

 

非递归可运行代码:

 

#include <stdio.h>  

#include <stdlib.h>  

#include <string.h>  

  

typedef struct node {  

    struct node *next;  

    int data;  

} node;  

  

void createList(node **head, int data)  

{  

    node *pre, *cur, *new;  

  

    pre = NULL;  

    cur = *head;  

  

    while (cur != NULL) {  

        pre = cur;  

        cur = cur->next;  

    }  

  

    new = (node *)malloc(sizeof(node));  

    new->data = data;  

    new->next = cur;  

  

    if (pre == NULL)      

        *head = new;  

    else  

        pre->next = new;       

}  

  

void printLink(node *head)  

{  

    while (head->next != NULL) {  

        printf("%d ", head->data);  

        head = head->next;  

    }  

    printf("%d\n", head->data);  

}  

  

int linkLen(node *head)  

{  

    int len = 0;  

      

    while (head != NULL) {  

        len ++;  

        head = head->next;  

    }  

  

    return len;  

}  

  

node* reverseK(node *head, int k)  

{  

    int i, len, time, now;  

      

    len = linkLen(head);  

  

    if (len < k) {  

        return head;  

    } else {  

        time = len / k;  

    }  

  

    node *newhead, *prev, *next, *old, *tail;  

  

    for (now = 0, tail = NULL; now < time; now ++) {  

        old = head;  

  

        for (i = 0, prev = NULL; i < k; i ++) {  

            next = head->next;  

            head->next = prev;  

            prev = head;  

            head = next;  

        }  

  

        if (now == 0) {  

            newhead = prev;  

        }  

          

        old->next = head;  

  

        if (tail != NULL) {  

            tail->next = prev;  

        }  

  

        tail = old;  

    }  

  

    if (head != NULL) {  

        tail->next = head;  

    }  

  

    return newhead;  

}  

  

  

int main(void)  

{  

    int i, n, k, data;  

    node *head, *newhead;  

  

    while (scanf("%d %d", &n, &k) != EOF) {   

        for (i = 0, head = NULL; i < n; i ++) {  

            scanf("%d", &data);  

            createList(&head, data);  

        }  

  

        printLink(head);  

  

        newhead = reverseK(head, k);      

  

        printLink(newhead);  

    }  

  

    return 0;  

}  

 

 

5、利用两个stack模拟queue

 

剑指offer上的原题,九度oj有专门的练习,这里贴一下我的ac代码:

 

#include <stdio.h>  

#include <stdlib.h>  

#include <string.h>  

   

typedef struct stack {  

    int top;  

    int seq[100000];  

} stack;  

   

/** 

 * 入队操作 

 

 * T = O(1) 

 

 */  

void pushQueue(stack *s1, int data)  

{  

    s1->seq[s1->top ++] = data;  

}  

   

/** 

 * 出队操作 

 

 * T = O(n) 

 

 */  

void popQueue(stack *s1, stack *s2)  

{  

    if (s2->top > 0) {  

        printf("%d\n", s2->seq[-- s2->top]);  

    } else {  

        while (s1->top > 0) {  

            s2->seq[s2->top ++] = s1->seq[-- s1->top];  

        }  

   

        if (s2->top > 0)   

            printf("%d\n", s2->seq[-- s2->top]);  

        else  

            printf("-1\n");  

    }  

   

}  

   

   

int main(void)  

{  

    int data, n;  

    stack *s1, *s2;  

    char str[5];  

   

    while (scanf("%d", &n) != EOF) {  

        // 初始化    

        s1 = (stack *)malloc(sizeof(stack));  

        s2 = (stack *)malloc(sizeof(stack));  

        s1->top = s2->top = 0;  

   

        while (n --) {  

            scanf("%s", str);  

   

            if (strcmp(str, "PUSH") == 0) { // 入队列  

                scanf("%d", &data);  

                pushQueue(s1, data);  

            } else {    // 出队列  

                popQueue(s1, s2);  

            }  

        }  

   

        free(s1);  

        free(s2);  

    }  

   

    return 0;  

}  

 

 

6、一个m*n的矩阵,从左到右从上到下都是递增的,给一个数elem,求是否在矩阵中,给出思路和代码

 

杨氏矩阵,简单题目:

 

#include <stdio.h>  

#include <stdlib.h>  

  

/** 

 * 有序矩阵查找 

 

 * T = O(n + n) 

 

 */  

void findKey(int **matrix, int n, int m, int key)  

{  

    int row, col;  

  

    for (row = 0, col = m - 1; row < n && col >= 0;) {  

        if (matrix[row][col] == key) {  

            printf("%d行,第%d\n", row + 1, col + 1);  

            break;  

        } else if (matrix[row][col] > key) {  

            col -= 1;  

        } else {  

            row += 1;  

        }  

    }  

    printf("不存在!\n");  

}  

  

int main(void)  

{  

    int i, j, key, n, m, **matrix;  

  

    // 构造矩阵  

    scanf("%d %d", &n, &m);  

    matrix = (int **)malloc(sizeof(int *) * n);  

    for (i = 0; i < n; i ++)  

        matrix[i] = (int *)malloc(sizeof(int) * m);  

  

    for (i = 0; i < n; i ++) {  

        for (j = 0; j < m; j ++)  

            scanf("%d", &matrix[i][j]);  

    }  

  

    // 查询数据  

    while (scanf("%d", &key) != EOF) {  

        findKey(matrix, n, m, key);       

    }  

  

    return 0;  

}  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多