CCF-CSP认证(202104)题目

202104-1(灰度直方图)

题目链接:计算机软件能力认证考试系统http://118.190.20.162/view.page?gpid=T128

题意:

        题目要求我们去求所给出矩阵中从0到L - 1 的出现的次数。

思路:

        直接用map就行。

代码:(简单,省略)

202104-2(邻域均值)(二维前缀和)

题目链接:

计算机软件能力认证考试系统http://118.190.20.162/view.page?gpid=T127题意:

        给个n * n矩阵,n, L, t, r给出,每个像素块都有自己的灰度,如果在领域的所有灰度的均值小于或等于t, 那么这个点就是暗点,记录所有暗点的数目。

思路:

        难点在于如何快速求出每个点对应邻域的均值。我们可以使用二维前缀和即可,剩下的处理就是细节问题了。

代码:
 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<cmath>
#include<unordered_set>
// #define int long long

using namespace std;

const int N = 650;

int n, L, r, t, ans;
int g[N][N];

void solve()
{
	cin >> n >> L >> r >> t;
	
	int x;
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= n; ++ j)
		{
			cin >> x;
			g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + x;
		}
	
	int x1, y1, x2, y2;
	
	for (int i = 1; i <= n; ++ i)
		for (int j = 1; j <= n; ++ j)
		{
			x1 = max(i - r, 1), y1 = max(j - r, 1);
			x2 = min(i + r, n), y2 = min(j + r, n);
			int q = x2 - x1 + 1, w = y2 - y1 + 1;
			
			int now = g[x2][y2] - g[x1 - 1][y2] - g[x2][y1 - 1] + g[x1 - 1][y1 - 1];
			int k = q * w;
			int res = now / k;
			if(now % k == 0)
			{
			    if(res <= t) ans ++;
			}
			else if(res < t) ans ++;
		}
		
	cout << ans << endl;
}

signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	
//	int T;
//	cin >> T;
//	
//	while(T--)
//	{
		solve();
//	}
	
	return 0;
}

202104-3(DHCP服务器)(大模拟)

题目链接:

计算机软件能力认证考试系统icon-default.png?t=N6B9http://118.190.20.162/view.page?gpid=T126题意:
        具体直接看题目即可,太长了不好概括,就是对每个请求进行相应。

思路:
        直接模拟就行,我看不进去,只写了20%的结果,也就是20分。

代码(满足前20%的样例):

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<cmath>
#include<unordered_set>
// #define int long long

using namespace std;

const int N = 210;

struct IP
{
    int state = 0;
    string host = "";
    int eti = 0;
} ips[N];

int x, t, tma, tmi, n;
string h;

void brush()
{
    for (int i = 1; i < x; ++ i)
    {
        if(ips[i].state == 1 && ips[i].eti == 0) ips[i].state = 2;
        else if(ips[i].state == 1) ips[i].eti --;
    }
}

void solve()
{
	cin >> x >> t >> tma >> tmi >> h;
	cin >> n;
	
	int tim;
	string a, b, c;
	int d, e;
	
	while(n --)
	{
		cin >> tim >> a >> b >> c >> d >> e;
		
		brush();
		
		int fdip = 0;
		bool is = true;
		
		if(b == "*" || b == h) 
		{
			for (int i = 1; i <= x && is; ++ i)
			    if(ips[i].state == 1) 
			    {
			        if(ips[i].host == a) fdip = i, is = false;
			    }
			
			for (int i = 1; i <= x && is; ++ i)
			    if(ips[i].state == 0) fdip = i, is = false;
			   
		    for (int i = 1; i <= x && is; ++ i)
		        if(ips[i].state == 2) fdip = i, is = false;
		        
		    if(fdip == 0) continue;
		    else 
		    {
		        ips[fdip].state = 1, ips[fdip].host = a;
		        if(e == 0) ips[fdip].eti = tim + t;
		        else
		        {
		            int las = tim + e;
		            if(las < tmi) ips[fdip].eti = tim + tmi;
		            else if(las > tma) ips[fdip].eti = tim + tma;
		            else ips[fdip].eti = tim + e;
		        }
		    }
		    
		    cout << h << ' ' << a << ' ' << "OFR" << ' ' << fdip << ' ' << ips[fdip].eti << endl;
		}
	}
}

signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	
    solve();
	
	return 0;
}

满分代码:
 

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int MAXSIZE = 10010;  
 
int n,Tdef,Tmax,Tmin,curTime;
struct IP
{
	int state;//未分配、待分配、占用、过期 0 1 2 3
	int t;/* 过期时间 */
	string owner;//占用主机名  处于未分配状态的 IP 地址没有占用者,而其余三种状态的 IP 地址均有一名占用者
}ip[MAXSIZE];
 
void upadate_ip_state()
{
	for(int i = 1;i<=n; i++)
		if(ip[i].t==0)
			continue;
		else if(ip[i].t<=curTime)
		{
			if(ip[i].state==1)
			{
				ip[i].state = 0;
				ip[i].owner = "";
				ip[i].t = 0;
			}
			else
			{
				ip[i].state = 3;
				ip[i].t = 0;
			}
		}
}
void initial_ip()
{
	for(int i = 1;i<n;i++)
	{
		ip[i].state = 0;
		ip[i].owner = "";
		ip[i].t = 0;
	}
	return ;
}
 
 
void test()
{
	for(int i = 1; i<n; i++)
		cout<<ip[i].state<<"\t";
	cout<<endl;
}
 
int get_suit_time(int t_p)
{
	if(t_p==0)
		return curTime+Tdef;
	else if(t_p<Tmin+curTime)
		return curTime+Tmin;
	else if(t_p>Tmax+curTime)
		return curTime+Tmax;
	else
		return t_p;
}
 
int get_ip_by_state(int s)
{
	for(int i = 1;i<n; i++)
		if(ip[i].state==s)
			return i;
	return 0;
}
 
 
int get_ip_by_owner(string own)
{
	for(int i = 1 ;i<n; i++)
		if(ip[i].owner==own)
			return i;
 
	return 0;
}
 
int main()
{
 
	string H;
	cin>>n>>Tdef>>Tmax>>Tmin>>H;
	n+=1;
	int len;
	cin>>len;
	initial_ip();
	while(len--)
	{
		string client,server,type;
		int target_ip,time;//过期时刻
		cin>>curTime>>client>>server>>type>>target_ip>>time;
		
		if(server!="*"&&server!=H)
			if(type!="REQ")
				continue;
		if(type!="DIS"&&type!="REQ") continue;
		if(server=="*"&&type!="DIS"||server==H&&type=="DIS") continue;
		upadate_ip_state();
		if(type=="DIS")
		{
			int k = get_ip_by_owner(client);
			if(!k) k = get_ip_by_state(0);
			if(!k) k = get_ip_by_state(3);
			if(!k)	continue;
			ip[k].state = 1;
			ip[k].owner = client;
 
			int opt_time = get_suit_time(time);
			ip[k].t = opt_time;
			cout<<"dhcp "<<client<<" OFR "<<k<<" "<<opt_time<<endl;
 
		}
		else if(type=="REQ")
		{
			if(server!=H)
			{
				int k = get_ip_by_owner(client);
				if(!k) continue;
				if(ip[k].state==1)
				{	
					ip[k].state=0;
					ip[k].t = 0;
					ip[k].owner = "";
 
				}
				continue;
			}
			int opt_time = get_suit_time(time);
			if(target_ip>=n||target_ip<=0||ip[target_ip].owner!=client)
			{	
				cout<<"dhcp "<<client<<" NAK "<<target_ip<<" 0"<<endl;
				continue;
			}
			ip[target_ip].state = 2;
			ip[target_ip].t = opt_time;
			cout<<"dhcp "<<client<<" ACK "<<target_ip<<" "<<opt_time<<endl;
 
		}
 
	}
	return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dawpro_加薪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值