ccf-DHCP服务器-50分

如果有同样没做出来的同学,可以的话我们讨论一下。
只有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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值