腾讯移动客户端开发暑期实习一面笔试

刚刚结束笔试,七点半开始,两个小时时间两道编程题。

第一题不会写,第二题快排写的有问题,肯定挂,通知是笔试结果显示是面试,我就准备了面试的东西,emm

第一题

要求:

  1. 推荐使用c++作答
  2. 考虑通用性,尽可能使用模板实现

题目:
基于链表实现一个List,需要包含的功能

  1. push_back() 结尾追加元素
  2. push_front() 在开始追加元素
  3. pop_back() 删除并返回结尾的元素
  4. pop_front() 删除并返回开头的元素
  5. size() 链表元素个数
  6. clear() 清空链表
  7. 需要有copy constructor(拷贝构造函数), assignment operator(赋值运算符)等特殊的函数
  8. reverse() 反转链表。 比如原来的元素为 1 2 3 4 5, 调用后变成 5 4 3 2 1

测试用例:
以Student作为测试用例,进行相关函数的测试。
struct Student {
int64_t id;
std::string name;
int32_t age;
};
// 测试用例供参考
list.push_back(Student{ 1, "hello", 24 });
list.push_back(Student{ 2, "world", 27 });
list.push_back(Student{ 3, "bob", 29 });
list.push_front(Student{ 4, "jason", 0 });
list.push_front(Student{ 5, "foo", 20 });
assert(5 == list.size());
assert(5 == list.pop_front().id);
assert(4 == list.size());
assert(3 == list.pop_back().id);
assert(3 == list.size());
list.reverse();
assert(2 == list.pop_front().id);
assert(4 == list.pop_back().id);

T copy_list1 = list;
T copy_list2{list};

assert(1 == copy_list1.size());
copy_list1.clear();
assert(0 == copy_list1.size());
assert(1 == list.size());

我的代码:

#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<cmath>
#include<list>

using namespace std;

struct Student
{
    int64_t id;
    std::string name;
    int32_t age;
}List[10010];

//list<Student> L;
int cnt=0;

void push_back(int id,string name,int age)
{
    List[cnt].id=id;
    List[cnt].name=name;
    List[cnt++].age=age;
}

void clear()
{
    for(int i=0;i<cnt;i++)
        List[i].id=List[i].age=0,List[i].name="";
}

void reverse()
{
    reverse(List,List+cnt);
}

void pop_back()
{
    cout<<List[cnt-1].id<<" "<<List[cnt-1].age<<" "<<List[cnt-1].name<<endl;
    List[cnt-1].id=List[cnt-1].age=0;
    List[cnt-1].name="";
}

void push_front(int id,string name,int age)
{
    for(int i=cnt;cnt>=1;cnt--)
    {
        List[i].id=List[i-1].id;
        List[i].age=List[i-1].age;
        List[i].name=List[i-1].name;
    }
    List[0].id=id;
    List[0].age=age;
    List[0].name=name;
}

void pop_front()
{
    cout<<List[0].id<<" "<<List[0].age<<" "<<List[0].name<<endl;
    for(int i=1;i<cnt;i++)
    {
        List[i-1].id=List[i].id;
        List[i-1].age=List[i].age;
        List[i-1].name=List[i].name;
    }
}

int main()
{

    // 结尾添加元素
    push_back(1,"hello",24);

    push_front(1,"hello",24); //在开始追加元素

    pop_back(); //删除并返回结尾的元素

    pop_front(); //删除并返回开头的元素

    int size=cnt;//size() //链表元素个数

    clear();

    reverse();

    return 0;
}

第2题

要求

  1. 不能使用标准库
  2. 输入数据范围 [-2^31, 2^31]
    题目:定义一个数组实现以下算法
  3. uniq() 删除重复元素
  4. 对数组进行排序
    测试用例:
    [2, 3, 9, 8, 9, 3, 5, 1,6, 3, -2147483648, 2147483647]

我的代码:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>

using namespace std;
typedef double ull;
#define inf 0x3f3f3f3f
ull a[100010];
bool book[100010];
int n;

void uniq()
{
    for(int i=1;i<n;i++)
    {
        if(a[i]==a[i-1])
            book[i]=1;
    }
}
//
//void quick_sort(int L,int R)
//{
//    int i=a[L],j=R;
//    while(a[j]>a[L]&&i<j) j--;
//    while(a[i]<a[L]&&i<j) i++;
//    if(j>i) swap(a[i],a[j]);
//    quick_sort(L,i-1);
//    quick_sort(i+1,R);
//}

void bubble_sort()
{
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                ull t=a[i];
                a[i]=a[j],a[j]=t;
            }
        }
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    //    quick_sort(0,n-1);
    bubble_sort();
    uniq();
    for(int i=0;i<n;i++)
    {
        if(!book[i])
            cout<<(long long)a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/OFSHK/p/14526298.html