#include <iostream>
using namespace std;
struct ip
{
int time;
int state; // 1:未分配 2:待分配 3:占用 4:过期
string name;
} ip[10001];
long long int time,go_time,seq,tdef,tmin,tmax;
long long int search()
{
if (time == 0) go_time = seq + tdef;
else if (tmin <= time - seq && time - seq <= tmax) go_time = time;
else if (time - seq < tmin) go_time = seq + tmin;
else if (time - seq > tmax) go_time = seq + tmax;
return(go_time);
}
int main()
{
int n;
string host;
cin >> n >> tdef >> tmax >> tmin >> host;
for (int i = 1; i <= n; i++) ip[i].state = 1;
int n2;
cin>>n2;
for (int k = 1; k <= n2; k++) {
string shost, get, type;
int ip2;
cin >> seq >> shost >> get >> type >> ip2 >> time;
for (int i = 1; i <= n; i++)
{
if (ip[i].state == 2 && ip[i].time <= seq)
{
ip[i].state = 1;
ip[i].name = "";
ip[i].time = 0;
} else if (ip[i].state == 3 && ip[i].time <= seq)
{
ip[i].state = 4;
ip[i].time = 0;
}
}
if (get != host && get != "*" && type != "REQ") continue;
if (type != "DIS" && type != "REQ") continue;
if ((get == "*" && type != "DIS") || (get == host && type=="DIS")) continue;
if (type == "DIS")
{
int select = -1;
for (int i = 1; i <= n; i++)
if (ip[i].name == shost)
{
select = i;
break;
}
for (int i = 1; i <= n; i++)
if (select==-1&&ip[i].state == 1)
{
select = i;
break;
}
for (int i = 1; i <= n; i++)
if (select==-1&&ip[i].state == 4)
{
select = i;
break;
}//这三个不能写到一个循环里面
if (select == -1) continue;
else
{
ip[select].state = 2;
ip[select].name = shost;
ip[select].time=search();
cout << host << " " << shost << " OFR " << " "<<select << " " << ip[select].time<<endl;
}
}
if (type == "REQ")
{
if (get != host)
{
for (int i = 1; i <= n; i++)
{
if (ip[i].name == shost && ip[i].state == 2)
{
ip[i].state = 1;
ip[i].name = "";
ip[i].time = 0;
break;
}
}
continue;
}
if (ip2>n ||ip[ip2].name != shost)
{
cout << host << " " << shost <<" "<< " NAK " << ip2 << " 0" << endl;
}
else
{
ip[ip2].state = 3;
ip[ip2].time = search();
cout << host << " " << shost <<" "<< " ACK " << ip2 << " " << ip[ip2].time << endl;
}
}
}
return 0;
}
ccf题解
最新推荐文章于 2023-04-19 04:35:44 发布