CCF 202104-3 DHCP服务器

40分

#include<iostream>
#include<string>
#define error -1
using namespace std;
int N,Tdef,Tmax,Tmin;
string H;
struct list {//不要修改
    int time;
    string host;
    string rec;
    string type;
    int ip;
    int end;
} text[205];

struct IP {
    string name = "";
    int state = 1;//未分配1、待分配2、占用3、过期4
    int end = 0;
} ip[205];

int FindIp(int k) {
    for(int i = 1 ; i <= N ; i++) { //检查是否有占用者为发送主机的 IP 地址
        if(ip[i].name == text[k].host) {
//            cout << ' ' << ip[i].name  << " " << text[k].host << '\n';
            return i;
        }
    }
    for(int i = 1 ; i <= N ; i++) { //最小的状态为未分配的 IP 地址
        if(ip[i].state == 1) {
            return i;
        }
    }
    for(int i = 1 ; i <= N ; i++) { //最小的状态为过期的 IP 地址
        if(ip[i].state == 4) {
            return i;
        }
    }
    return error;
}

void Dis(int i) {
    int IP = FindIp(i);
    if(IP == error) return;
    ip[IP].state = 2,ip[IP].name = text[i].host;
    if(text[i].end == 0) { //过期时刻为0
        ip[IP].end = text[i].time + Tdef;
    } else {
        if(Tmin <= text[i].end - text[i].time && text[i].end - text[i].time <= Tmax) {
            ip[IP].end = text[i].end;
        } else if(Tmin > text[i].end - text[i].time) {
            ip[IP].end = Tmin + text[i].time ;
        } else if(text[i].end - text[i].time > Tmax) {
            ip[IP].end = Tmax + text[i].time ;
        }
    }
    cout << H << ' ' << text[i].host << ' ' << "OFR" << ' ' << IP << ' ' << ip[IP].end << '\n';
}

//对于 Request 报文,按照下述方法处理:
void Req(int i) {
    if(text[i].rec != H) {//检查接收主机是否为本机
        for(int k = 1 ; k <= N ; k++) {
            if(ip[k].name == text[i].host) {
                if(ip[k].state == 2) {
                    ip[k].state = 1;
                    ip[k].end = 0;
                    ip[k].name = "";
                    return;
                }
            }
        }
    }
    int IP = FindIp(i);
    if(IP == error) {
        cout << H << ' ' << text[i].host << ' ' << "NAK" << ' ' << text[i].ip << ' ' << 0 << '\n';
        return;
    }
    if(text[i].ip < 1 || text[i].ip > N || ip[IP].name != text[i].host) {
        cout << H << ' ' << text[i].host << ' ' << "NAK" << ' ' << IP << ' ' << ip[IP].end << '\n';
        return;
    }
    ip[IP].state = 3;
    if(text[i].end == 0) { //过期时刻为0
        ip[IP].end = text[i].time + Tdef;
    } else {
        if(Tmin <= text[i].end - text[i].time && text[i].end - text[i].time <= Tmax) {
            ip[IP].end = text[i].end;
        } else if(Tmin > text[i].end - text[i].time) {
            ip[IP].end = Tmin + text[i].time ;
        } else if(text[i].end - text[i].time > Tmax) {
            ip[IP].end = Tmax + text[i].time ;
        }
    }
    cout << H << ' ' << text[i].host << ' ' << "ACK" << ' ' << IP << ' ' << ip[IP].end << '\n';

}
void updateIp(int time) {
    for(int k = 1 ; k <= N ; k++) {
        if(ip[k].end <= time) {
            if(ip[k].state == 2) {
                ip[k].state = 1;
            } else if(ip[k].state == 3) {
                ip[k].state = 4;
            }
        }
    }
    return;
}
int main() {
    cin >> N >> Tdef >> Tmax >> Tmin >> H;
    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++) {
        cin >> text[i].time >> text[i].host >> text[i].rec >> text[i].type >> text[i].ip >> text[i].end;
    }
    for(int i = 0 ; i< n ; i++) {
        updateIp(text[i].time);
        if(text[i].type == "DIS" && text[i].rec == "*") {
//            cout << text[i].time <<"          ";
            Dis(i);
        } else if(text[i].type == "REQ" && text[i].rec != "*") {
//            cout << text[i].time <<"          ";
            Req(i);
        }
    }
}

 70分

#include<iostream>
#include<string>
#define error -1
using namespace std;
int N,Tdef,Tmax,Tmin;
string H;
struct list {//不要修改
    int time;
    string host;
    string rec;
    string type;
    int ip;
    int end;
} text[10005];

struct IP {
    string name = "";
    int state = 1;//未分配1、待分配2、占用3、过期4
    int end = 0;
} ip[10005];

int FindIp(int k) {
    for(int i = 1 ; i <= N ; i++) { //检查是否有占用者为发送主机的 IP 地址
        if(ip[i].name == text[k].host) {
//            cout << ' ' << ip[i].name  << " " << text[k].host << '\n';
            return i;
        }
    }
    for(int i = 1 ; i <= N ; i++) { //最小的状态为未分配的 IP 地址
        if(ip[i].state == 1) {
            return i;
        }
    }
    for(int i = 1 ; i <= N ; i++) { //最小的状态为过期的 IP 地址
        if(ip[i].state == 4) {
            return i;
        }
    }
    return error;
}

int FindIp2(int k) {
    for(int i = 1 ; i <= N ; i++) { //检查是否有占用者为发送主机的 IP 地址
        if(ip[i].name == text[k].host && ip[i].state != 1 && ip[i].state != 1) {
//            cout << ' ' << ip[i].name  << " " << text[k].host << '\n';
            return i;
        }
    }
    return error;
}

void Dis(int i) {
    int IP = FindIp(i);
    if(IP == error) return;
    ip[IP].state = 2,ip[IP].name = text[i].host;
    if(text[i].end == 0) { //过期时刻为0
        ip[IP].end = text[i].time + Tdef;
    } else {
        if(Tmin <= text[i].end - text[i].time && text[i].end - text[i].time <= Tmax) {
            ip[IP].end = text[i].end;
        } else if(Tmin > text[i].end - text[i].time) {
            ip[IP].end = Tmin + text[i].time ;
        } else if(text[i].end - text[i].time > Tmax) {
            ip[IP].end = Tmax + text[i].time ;
        }
    }
    cout << H << ' ' << text[i].host << ' ' << "OFR" << ' ' << IP << ' ' << ip[IP].end << '\n';
}

//对于 Request 报文,按照下述方法处理:
void Req(int i) {
    if(text[i].rec != H) {//检查接收主机是否为本机
        for(int k = 1 ; k <= N ; k++) {
            if(ip[k].name == text[i].host) {
                if(ip[k].state == 2) {
                    ip[k].state = 1;
                    ip[k].end = 0;
                    ip[k].name = "";
                    return;
                }
            }
        }
    }
    int IP = FindIp2(i);
    if(IP == error || text[i].ip < 1 || text[i].ip > N  || text[i].ip != IP) {
        cout << H << ' ' << text[i].host << ' ' << "NAK" << ' ' << text[i].ip << ' ' << 0 << '\n';
        return;
    }
    if(ip[IP].name != text[i].host) {
        cout << H << ' ' << text[i].host << ' ' << "NAK" << ' ' << IP << ' ' << ip[IP].end << '\n';
        return;
    }
    ip[IP].state = 3;
    if(text[i].end == 0) { //过期时刻为0
        ip[IP].end = text[i].time + Tdef;
    } else {
        if(Tmin <= text[i].end - text[i].time && text[i].end - text[i].time <= Tmax) {
            ip[IP].end = text[i].end;
        } else if(Tmin > text[i].end - text[i].time) {
            ip[IP].end = Tmin + text[i].time ;
        } else if(text[i].end - text[i].time > Tmax) {
            ip[IP].end = Tmax + text[i].time ;
        }
    }
    cout << H << ' ' << text[i].host << ' ' << "ACK" << ' ' << IP << ' ' << ip[IP].end << '\n';

}
void updateIp(int time) {
    for(int k = 1 ; k <= N ; k++) {
        if(ip[k].end <= time) {
            if(ip[k].state == 2) {
                ip[k].state = 1;
            } else if(ip[k].state == 3) {
                ip[k].state = 4;
            }
        }
    }
    return;
}
int main() {
    cin >> N >> Tdef >> Tmax >> Tmin >> H;
    int n;
    cin >> n;
    for(int i = 0 ; i < n ; i++) {
        cin >> text[i].time >> text[i].host >> text[i].rec >> text[i].type >> text[i].ip >> text[i].end;
    }
    for(int i = 0 ; i< n ; i++) {
        updateIp(text[i].time);
        if(text[i].type == "DIS" && text[i].rec == "*") {
//            cout << text[i].time <<"          ";
            Dis(i);
        } else if(text[i].type == "REQ" && text[i].rec != "*") {
//            cout << text[i].time <<"          ";
            Req(i);
        }
    }
}

实在改不出来了 , 看看(栗山)未来能不能帮帮我QWQ

posted @ 2022-07-14 20:11  夏莱发电厂的Sensei  阅读(59)  评论(0)    收藏  举报