202104-3DHCP服务器

简介: 202104-3DHCP服务器

本题链接 DHCP服务器

本博客给出本题截图

1.jpg

C++

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m, t_def, t_max, t_min;
string h;
struct IP
{
    int state; 
    int t;  
    string owner;
}ip[N];
void update_ips_state(int tc)
{
    for (int i = 1; i <= n; i ++ )
        if (ip[i].t && ip[i].t <= tc)
        {
            if (ip[i].state == 1)
            {
                ip[i].state = 0;
                ip[i].owner = "";
                ip[i].t = 0;
            }
            else
            {
                ip[i].state = 3;
                ip[i].t = 0;
            }
        }
}
int get_ip_by_owner(string client)
{
    for (int i = 1; i <= n; i ++ )
        if (ip[i].owner == client)
            return i;
    return 0;
}
int get_ip_by_state(int state)
{
    for (int i = 1; i <= n; i ++ )
        if (ip[i].state == state)
            return i;
    return 0;
}
int main()
{
    cin >> n >> t_def >> t_max >> t_min >> h;
    cin >> m;
    while (m -- )
    {
        int tc;
        string client, server, type;
        int id, te;
        cin >> tc >> client >> server >> type >> id >> te;
        if (server != h && server != "*")
        {
            if (type != "REQ") continue;
        }
        if (type != "DIS" && type != "REQ") continue;
        if (server == "*" && type != "DIS" || server == h && type == "DIS") continue;
        update_ips_state(tc);
        if (type == "DIS")
        {
            int k = get_ip_by_owner(client);
            if (!k) k = get_ip_by_state(0);
            if (!k) k = get_ip_by_state(3);
            if (!k) continue;
            ip[k].state = 1, ip[k].owner = client;
            if (!te) ip[k].t = tc + t_def;
            else
            {
                int t = te - tc;
                t = max(t, t_min), t = min(t, t_max);
                ip[k].t = tc + t;
            }
            cout << h << ' ' << client << ' ' << "OFR" << ' ' << k << ' ' << ip[k].t << endl;
        }
        else
        {
            if (server != h)
            {
                for (int i = 1; i <= n; i ++ )
                    if (ip[i].owner == client && ip[i].state == 1)
                    {
                        ip[i].state = 0;
                        ip[i].owner = "";
                        ip[i].t = 0;
                    }
                continue;
            }
            if (!(id >= 1 && id <= n && ip[id].owner == client))
                cout << h << ' ' << client << ' ' << "NAK" << ' ' << id << ' ' << 0 << endl;
            else
            {
                ip[id].state = 2;
                if (!te) ip[id].t = tc + t_def;
                else
                {
                    int t = te - tc;
                    t = max(t, t_min), t = min(t, t_max);
                    ip[id].t = tc + t;
                }
                cout << h << ' ' << client << ' ' << "ACK" << ' ' << id << ' ' << ip[id].t << endl;
            }
        }
    }
    return 0;
}

总结

大模拟题,近几年csp认证考试中比较简单的一道模拟题

阅读理解 + 细致

适当的备注可以减少我们后期写代码时的混乱

逐行翻译内容,按照要求写代码

目录
相关文章
|
6月前
|
存储 安全 Unix
一文带你了解服务器!
一文带你了解服务器!
73 1
|
6月前
在选择服务器上
在选择服务器上
33 1
|
7月前
|
存储 缓存 监控
服务器的介绍
服务器的介绍
114 0
|
域名解析 Ubuntu 物联网
服务器使用
服务器使用
|
存储 Linux 数据库
第一次使用服务器
从了解服务器,到部署服务器,虽然看起来是简简单单的一些基本操作,但是对于初学者来说,是一次不小的进步,我会把我第一次领服务器和部署服务器的体会卸载如下的文档中。
174 0
|
NoSQL 关系型数据库 MySQL
使用服务器后的心得
在学习使用服务器后的一些感想和收获
|
弹性计算 安全 Java
服务器
我是一名计算机专业的学生,目前一年级。在实训的时候老师推荐我们参加阿里云的飞天加速计划领取免费的服务器,试用时间为两周。领取前我在阿里云上完成了学生认证和新手预备训练营,并且学习使用了阿里云的镜像快照。在实训的时候我应用阿里云的用户和密码实现了远程超控虚拟机,在老师的一步步的教导下,我完成了我的第一个网址,实现了第一个成果。飞天加速计划帮助了许多高校学生接触到了云服务器,免费学到了许多知识。我以后也会随着课程的推进,利用阿里云服务器完成每一项课程作业,学习到更多的知识。我们很难有机会去接触到这方面的内容。但是通过阿里云飞天加速计划 让我了解和使用阿里云服务器,在使用和学习中了解了很 多知识。
|
弹性计算 Java 应用服务中间件
服务器很好用
作为一名大学生,我很高兴能够参加“飞天加速计划-高校学生在家实践”活动,并且使我受益匪浅,除了可以免费申领云服务器意外,在这一个多星期的体验时间里,我感受到了阿里云ECS的方便与快捷,例如可以随意更改操作系统,对初学者极为友善等等。
|
Java 关系型数据库 MySQL
第一次如何快速了解服务器
总结一下,自己这7天左右时间对于阿里云服务器的了解
|
安全 网络安全 数据库

热门文章

最新文章