c++ 笔试题2

1.socket中能传输结构体吗?

不能,因为结构体的大小与内存对齐,不能保证发送方和接收方是同样的机器同样的系统,传输结构体的话会导致数据紊乱。

2.实现一个队列,使用单链表还是双链表好?如何实现?

使用双向链表好,因为单链表不便在末尾添加元素。可通过list来代理实现,基体的代码如下:

#ifndef _QUEUE
#define _QUEUE
#pragma once
#include <list>
#include <iostream>
using std::list;
using std::cout;
using std::endl;
 
typedef int Data;
typedef list<Data>::iterator iterator;
typedef list<Data>::const_iterator const_iterator;
typedef list<Data>::size_type size_type;
class Queue
{
private:
    list<Data> l;
public:
    bool isEmpty() {
        return l.empty();
    }
    void push(const Data &data) {
        l.push_back(data);
    }
    //出队列
    Data& deQueue() {
        Data &data = l.front();
        l.pop_front();
        return data;
    }
    //取队头元素
    Data& peek() {
        return l.front();
    }
   
    Queue(){}
    ~Queue(){}
};
 
#endif
 
void testQueue() {
    Queue q;
    q.push(1);
    q.push(3);
    q.push(5);
    q.push(7);
    cout << q.peek() << endl;
    q.deQueue();
    cout << q.peek() << endl;
    q.deQueue();
    cout << q.peek() << endl;
}


3.选择1、4、5中的的任意个数使它们的和为100,共有多少种选择方案?

int num[] = {1,4,5};
int getSum(int n) {
    int count = 0;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            for (int k = 0; k < n; k++) {
                if (num[0] * i + num[1] * j + num[2] * k == n) {
                     //cout << num[0] << "*" << i << "+" << num[1] << "*" << j << "+" << num[2] << "*" << k << "=" << num[0] * i + num[1] * j + num[2] * k << endl;
                    count++;
                }
            }
        }
    }
    return count;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘(MoChen)

爱打赏的人技术成长更快哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值