CSP-202104

目录

灰度直方图

邻域均值

 DHCP服务器


灰度直方图

#include <iostream>
using namespace std;

int main(){
	int n, m;
	int L;
	//int arr[520][520];
	int h[260];
	cin >> n >> m;
	cin >> L;
	int** arr = new int* [n]; // 首先分配rows个指针

	for (int i = 0; i < n; ++i) {
		arr[i] = new int[m]; // 再为每一行分配cols个整数空间
	}
	for (int i = 0; i < 260; i++) {
		h[i] = 0;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> arr[i][j];
			h[arr[i][j]]++;
		}
	}
	for (int i = 0; i < L; i++) {
		cout << h[i] << " ";
	}
	cout << endl;
	return 0;
}

邻域均值

#include<iostream>
using namespace std;

int main() {
	int n, L, r;
	int t;
	cin >> n >> L >> r;
	cin >> t;
	int** arr=new int* [610];
	int** s = new int* [610];
	for (int i = 0; i < 610; i++) {
		arr[i] = new int[610];
		s[i] = new int[610];
	}
	for (int i = 0; i < 610; i++) {
		s[0][i] = 0;
		s[i][0] = 0;
	}
	for (int i = 1; i <= n ; i++) {//计算二维前缀和
		for (int j = 1; j <= n ; j++) {
			cin >> arr[i][j];
			s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]+arr[i][j];
		}
	}
	int res = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			int x1, y1, x2, y2;
			x1 = max(1, i - r);
			y1 = max(1, j - r);
			x2 = min(n, i + r);
			y2 = min(n, j + r);
			int sum = s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1];
			int num = (x2 - x1 + 1) * (y2 - y1 + 1);
			if (sum <= num * t) {
				res++;
			}
		}
	}
	/*
	for (int i = 1; i <=n; i++) {
		for (int j = 1; j <= n; j++) {
			int sum = 0, num = 0;
			for (int m = i - r; m <= i + r; m++) {
				for (int k = j - r; k <= j + r; k++) {
					if (m >= 1 && m <= n  && k >= 1 && k <= n ) {
						sum += arr[m][k];
						num++;
					}
				}
			}//
			//cout << sum/num << " ";
			if (sum <= t*num) {
				res++;
			}
		}
		//cout << endl;
	}
	*/
	cout << res ;
	return 0;
}

 DHCP服务器

#include<iostream>
using namespace std;

int N, Tdef, Tmax, Tmin, n;//n表示收到了n个报文
//N表示能够分配给客户端的 IP 地址的数目,且能分配的 IP 地址
//Tdef默认过期时间  Tmax、Tmin表示分配给客户端的 IP 地址的最长过期时间长度和最短过期时间长度
string H;//本机名称


struct IP {
	int state;//1未分配、2待分配、3占用、4过期
	string owner;//占用者
	int t_guo;//过期时刻
}ip[10010];


int main() {
	cin >> N >> Tdef >> Tmax >> Tmin >> H>>n;
	//初始化 IP 地址池
	for (int i = 1; i <= N; i++) {
		ip[i].state = 1;
		ip[i].owner = "";
		ip[i].t_guo = 0;
	}
	while (n--) {
		int t;//收到报文的时刻
		string sender, receiver, type;
		int IP_P;//报文中的IP 地址
		int t_out;//过期时刻
		cin >> t >> sender >> receiver >>type>> IP_P >> t_out;
		
		if (receiver != H && receiver != "*" && type != "REQ")continue;
		if (type != "DIS" && type != "REQ")continue;
		if (receiver == "*" && type != "DIS" || receiver == H && type == "DIS")continue;
		//注意不要把N、n混淆!!!
		//更新IP地址池
		for (int i = 1; i <= N; i++) {
			//if (ip[i].t_guo && t >= ip[i].t_guo) {
				if (ip[i].state == 2 && t >= ip[i].t_guo) {
					ip[i].state = 1;
					ip[i].owner = "";
					ip[i].t_guo = 0;
				}
				else if(ip[i].state == 3 && t >= ip[i].t_guo) {
					ip[i].state = 4;
					ip[i].t_guo = 0;
				}
			//}
		}
		if (type == "DIS") {
			int chose_ip = 0;
			for (int i = 1; i <= N; i++) {
				if (ip[i].owner == sender) {
					chose_ip = i;
					break;
				}
			}
			if (chose_ip == 0) {
					for (int i = 1; i <= N; i++) {
						if (ip[i].state == 1) {
							chose_ip = i;
							break;
						}
					}
			}
			if (chose_ip == 0) {
					for (int i = 1; i <= N; i++) {
						if (ip[i].state == 4) {
							chose_ip = i;
							break;
						}
					}
			}
			if (chose_ip == 0)continue;
			ip[chose_ip].state = 2;
			ip[chose_ip].owner = sender;
			if (t_out == 0) { 
				ip[chose_ip].t_guo = t + Tdef; 
			}
			else {
				if (t_out - t > Tmax)   ip[chose_ip].t_guo = t + Tmax;
				else if(Tmin<=t_out - t&& t_out - t<=Tmax)  ip[chose_ip].t_guo = t_out;
				else if(t_out - t<Tmin)  ip[chose_ip].t_guo = t + Tmin;
			}
			cout << H << " " << sender << " " << "OFR " << chose_ip << " " << ip[chose_ip].t_guo << endl;
		}
		
		else if (type == "REQ") {
			if (receiver != H) {
				for (int i = 1; i <= N; i++) {
					if (ip[i].owner == sender&&ip[i].state ==2) {
						ip[i].state = 1;
						ip[i].t_guo = 0;
						ip[i].owner = "";
					}
				}
			}
			else {
				if (1 <= IP_P && IP_P <= N && ip[IP_P].owner == sender) {
					ip[IP_P].state = 3;
					if (t_out == 0) {
						ip[IP_P].t_guo = t + Tdef;
					}
					else {
						if (t_out - t > Tmax)   ip[IP_P].t_guo = t + Tmax;
						else if (Tmin <= t_out - t && t_out - t <= Tmax)  ip[IP_P].t_guo = t_out;
						else if (t_out - t < Tmin)  ip[IP_P].t_guo = t + Tmin;
					}
					cout << H << " " << sender << " " << "ACK " << IP_P << " " << ip[IP_P].t_guo << endl;
				}
				else {
					cout << H << " " << sender << " " << "NAK " << IP_P << " " << 0 << endl;
				}
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值