广州某游戏公司的面试题

来源:互联网 发布:指数基金和etf知乎 编辑:程序博客网 时间:2024/06/10 05:16
广州某游戏公司的面试题。
1、类似于约瑟夫环问题

2、字符串逆序打印,例如输入: I love this game   逆序打印  game this love I 


代码如下:

/***假设有N个小孩围在一起,组成一个环,一次进行编号。然后进行报数,数到3的人自动退出,然后从0重新开始报数,依次循环,求最后留下的小孩的原始编号这道题目是约瑟夫环问题,我用循环链表实现。author: lybdate:  2014-4-10**/#include <iostream>#include <malloc.h>using namespace std;const int MAX = 36;      // 小孩的人数const int N = 3;         // 间距,也就是数到N就退出,然后重新开始数typedef struct Node{    int num;             // 小孩初始编号    Node *next;}LinkList;void init(LinkList *l, int max){    LinkList *head = l;    head->num=1;                    // 第一个小孩的编号    LinkList *current=l;    // 创建max个节点    for (int i=2; i<=max ; ++i)    {        LinkList *lnode = (LinkList*)malloc(sizeof(LinkList));        lnode->num = i;        lnode->next = head;        current->next = lnode;        current = current->next;    }}int go (LinkList *l, int max, int n){    LinkList *p = l;    LinkList *front = NULL;    int exit = 0;     // 退出的人数    while(exit<max-1)    {        // 指针每移动n-1次,就free掉一个节点        for (int i=1; i<n; ++i)        {            front = p;            p = p->next;        }        // free        cout<<"delete "<<p->num<<endl;          // 打印每一轮淘汰的小孩编号        front->next = p->next;        free(p);        p = front->next;        exit++;    }    return p->num;}int main(){    LinkList children;    init(&children, MAX);          // 初始化生成MAX个节点    cout<<"The luck boy: "<<go(&children, MAX, N)<<endl;;    return 0;}


// 输入一行字符,将单词逆序输出,例如 I love this game, 输出为 game this love I// 用栈实现// author: lyb// date: 2014-4-10#include <iostream>using namespace std;const int MAX = 100;typedef struct{    char data[MAX][MAX/2];    int top;}Stack;// 对栈的操作,暂时不做判空,溢出判断void push(Stack *s, char *str){    int j=0;    for (int i=0; str[i]!='\0'; ++i)    {        if (str[i]!=' ')        {            s->data[s->top][j++] = str[i];        }        else        {            s->data[s->top][j] = '\0';            ++s->top;            j=0;        }    }    s->data[s->top][j]='\0';   // 对最后一个单词加字符串结束符}void pop(Stack *s){    while(s->top>=0)    {        cout<<s->data[s->top]<<" ";        s->top--;    }}int main(){    char *str = "Hello World";    Stack s;    s.top=0;    push(&s,str);    pop(&s);    return 0;}


欢迎交流。

from   lyb  2014-4-10



1 0
原创粉丝点击