如果有同样没做出来的同学,可以的话我们讨论一下。
只有50分,两个测试样例过了
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int N, Tdef, Tmax, Tmin;
string Dname;
int ti, want_ip, t_out;//过期时刻,不是时间段
string t_gust_name, t_host_name, mode;
struct IP {
int id;//ip地址
string gust_name;//对应的客户端名称
int mode=0;//0 未分配,1 待分配,2 已分配, 3 已过期
int time_e=0;//ip的过期时刻=ti+Tdef
};
int main() {
cin >> N >> Tdef >> Tmax >> Tmin >> Dname;
IP* Ip = new IP[N + 1];
for (int i = 1; i <= N; i++)//ip地址初始化
Ip[i].id = i;
int k; cin >> k;//报文次数
for (int j = 1; j <= k; j++) {
int give_ip = -1;
int g_t_out=0;
cin >> ti >> t_gust_name >> t_host_name >> mode >> want_ip >> t_out;
if ( (mode == "DIS")&&(t_host_name=="*") ) {
//先对DHCP服务器的IP进行检查
//待分配和已分配的过时处理不一样
for (int i = 1; i <= N; i++) {
if (Ip[i].mode == 1 || Ip[i].mode == 2) {
if (ti >= Ip[i].time_e) {
if (Ip[i].mode == 1) {
Ip[i].mode = 0;
Ip[i].gust_name = "";
}
else {
Ip[i].mode = 3;
}
Ip[i].time_e = 0;
}
}
}
//开始分配ip的1种情况
for (int i = 1; i <= N; i++) {
// IP 地址占用者为发送主机
if (Ip[i].gust_name == t_gust_name) {
give_ip = Ip[i].id;
Ip[i].mode = 1;
if (t_out == 0)g_t_out=Ip[i].time_e = ti + Tdef;
if (t_out > ti + Tmax)g_t_out = Ip[i].time_e = ti + Tmax;
if (t_out < ti + Tmin)g_t_out = Ip[i].time_e = ti + Tmin;
else { g_t_out = Ip[i].time_e = t_out; }
break;
}
}
//开始分配ip的2种情况
for (int i = 1; i <= N; i++) {
if (give_ip != -1)break;
if (Ip[i].mode == 0) {
give_ip = Ip[i].id;
Ip[i].mode = 1;
Ip[i].gust_name = t_gust_name;
if (t_out == 0)g_t_out = Ip[i].time_e = ti + Tdef;
if (t_out > ti + Tmax)g_t_out = Ip[i].time_e = ti + Tmax;
if (t_out < ti + Tmin)g_t_out = Ip[i].time_e = ti + Tmin;
else { g_t_out = Ip[i].time_e = t_out; }
break;
}
}
//开始分配ip的3种情况
for (int i = 1; i <= N; i++) {
if (give_ip != -1)break;
if (Ip[i].mode == 3) {
give_ip = Ip[i].id;
Ip[i].mode = 1;
Ip[i].gust_name = t_gust_name;
if (t_out == 0)g_t_out = Ip[i].time_e = ti + Tdef;
if (t_out > ti + Tmax)g_t_out = Ip[i].time_e = ti + Tmax;
if (t_out < ti + Tmin)g_t_out = Ip[i].time_e = ti + Tmin;
else { g_t_out = Ip[i].time_e = t_out; }
break;
}
}
if (give_ip != -1) {
cout << Dname << " " << t_gust_name << " " << "OFR" << " " << give_ip << " " << g_t_out << endl;
}
}
if (mode == "REQ") {
int fflag = 0;//0 不输出,1ACK输出,2NAk输出
//想要的Ip不满足条件
//先对DHCP服务器的IP进行检查
//待分配和已分配的过时处理不一样
for (int i = 1; i <= N; i++) {
if (Ip[i].mode == 1 || Ip[i].mode == 2) {
if (ti >= Ip[i].time_e) {
if (Ip[i].mode == 1) {
Ip[i].mode = 0;
Ip[i].gust_name = "";
}
else {
Ip[i].mode = 3;
}
Ip[i].time_e = 0;
}
}
}
//对别的服务器的请求
if (t_host_name != Dname&&t_host_name!="*") {
for (int i = 1; i <= N; i++) {
if ((Ip[i].gust_name == t_gust_name) && (Ip[i].mode == 1)) {
Ip[i].mode = 0;
Ip[i].gust_name = "";
Ip[i].time_e = 0;
}
}
}
//对自己服务器的请求
if (t_host_name == Dname) {
for (int i = 1; i <= N; i++) {
if ((Ip[i].id == want_ip) && (Ip[i].gust_name == t_gust_name)) {
fflag = 1;
Ip[i].mode = 2;
if (t_out == 0)g_t_out = Ip[i].time_e = ti + Tdef;
if (t_out > ti + Tmax)g_t_out = Ip[i].time_e = ti + Tmax;
if (t_out < ti + Tmin)g_t_out = Ip[i].time_e = ti + Tmin;
else { g_t_out = Ip[i].time_e = t_out; }
break;
}
}
if(fflag==0){ fflag = 2; }
}
if (fflag == 1) {
cout << Dname << " " << t_gust_name << " " << "ACK" << " " << want_ip << " " << g_t_out << endl;
}
if(fflag==2) {
g_t_out = 0;
cout << Dname << " " << t_gust_name << " " << "NAK" << " " << want_ip << " " << g_t_out << endl;
}
}
}
return 0;
}