乐其远程笔试题

  最近为找实习忙的四脚朝天。贴一贴乐其公司的远程笔试题吧。


1. 实现方法int[] intersect(int[] a, int[]b); 返回a与b的交集,a与b都是一个从小到大的排序数组,请实现该方法。

思路:常规的数组处理题。既然经排好序了,那就从左到右一一比较下去,统计相同数的对数。

void intersect(int dest[], int* size, int a[], int len_a, int b[], int len_b)
{
    int i = 0;
    int j = 0;
    int k = 0;
    while (i < len_a && j < len_b){
        if (a[i] == b[j]){
            dest[k] = a[i];
            ++i;
            ++j;
            ++k;
        }
        else if(a[i] > b[j]){
            j++;
        }
        else{
            i++;
        }
    }
    *size = k;
}

  

2. 一个无序的数组int src[n],查找数组中最大的3个元素,并给出时间复杂度。

 思路:常规的算法题。全排序的话复杂度会上升到O(nlog(n)),为了降低时间复杂度,采用最笨的方法,循环遍历三遍,每一遍都找出最大元素(剔除之前找到的)。时间复杂度降为O(log(n))。应该有更好的解法。

void find_max_3(int src[], int n)
{
    int max1 = src[0];
    for (int i = 0; i < n; ++i){
        if (src[i] > max1){
            max1 = src[i];
        }
    }
    cout << max1 << endl;

    int max2 = src[0];
    for (int i = 0; i < n; ++i){
        if (src[i] > max2 && src[i] != max1){
            max2 = src[i];
        }
    }
    cout << max2 << endl;

    int max3 = src[0];
    for (int i = 0; i < n; ++i){
        if (src[i] > max3 && src[i] != max1 && src[i] != max2){
            max3 = src[i];
        }
    }
    cout << max3 << endl;
}

3. 给定一个M*M的矩阵,请写一个方法给出它的螺旋式输出。

例:

1 2 3

4 5 6

7 8 9

输出为 123698745

思路:考察循环。采用从具体到抽象的思考方法,先写最外层,然后把跟层数有关的常量用变量替代。

void square_output(int** matrix, int M)
{
    for (int level = 0; level < M; ++level){
        for (int col = level; col < M-level; ++col){
            cout << matrix[level][col] << " ";
        }
        for (int row = level+1; row < M-level; ++row){
            cout << matrix[row][M-level-1] << " ";
        }
        for (int col = M-level-2; col >= level; --col){
            cout << matrix[M-level-1][col] << " ";
        }
        for (int row = M-level-2; row > level; --row){
            cout << matrix[row][level] << " ";
        }
    }
}
 

4. 有一个数组,其中除了两个数字以外,所有数字都是成对的, 请写一个算法,把其中两个不成对的数找出来。例如:[2 8 10 10 27 ] 需要把 [8 7] 找出。

思路:嵌套循环。时间复杂度O(n^2),应该有更好的方法。

void find_no_pair(int src[], int M)
{
    for (int i = 0; i < M; ++i){
        int j;
        for (j = 0; j < M; ++j){
            if (src[i] == src[j] && i != j){
                break;
            }
        }
        if (j == M){
            cout << src[i] << " ";
        }
    }
}

5.

<span style="font-size: 18px;">$test = (object)array(
     'drama' => 'House of Cards',
     'season' => 'Second'
     'episode' => 13
);
if(!$test['stars']) {
     $test['stars'] = 'Kevin Spacey, RobinWright, Kate Mara, and Corey Stoll ...';
}</span>

上面的代码好像有点问题,请让它能够运行起来。

思路:考察php基础语法。

<span style="font-size:14px;">$test = array(
     'drama' => 'House of Cards',
     'season' => 'Second',
     'episode' => 13,
     'stars' => ''
);
if (!$test['stars']) {
     $test['stars'] = 'Kevin Spacey, Robin Wright, Kate Mara, and Corey Stoll ...';
}
 </span>

6.  设计一个 drama 数据表存储美剧信息,创建一个 Drama类读取美剧基本信息,例如剧名、主演、集数,并把这些基本信息打印出来。

思路:考察面向对象编程思想。

class Drama
{
private:
    string drama_name;
    string star;
    int episodes;

public:
    Drama();
    ~Drama();
    void set_name(string name);
    void set_star(string star);
    void set_episode(int episode);

    string get_name();
    string get_star();
    int get_episodes();
};

Drama::Drama()
{
    drama_name = "";
    star = "";
    episodes = 0;
}

Drama::~Drama()
{
    // do nothing
}

int Drama::get_episodes(){
    return this->episodes;
}

string Drama::get_name(){
    return this->drama_name;
}

string Drama::get_star(){
    return this->star;
}

void Drama::set_name(string name)
{
    drama_name = name;
}

void Drama::set_star(string star)
{
    this->star = star;
}
void Drama::set_episode(int episode)
{
    this->episodes = episode;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值