洛谷打题记录(函数,选址)

文章描述了使用C++编写的三个程序片段,分别涉及二维数组的遍历、动态规划计算子矩阵和的方法以及路径跟踪问题。第一段代码计算给定二维数组中周围元素之和的最大值,第二段代码使用动态规划解决类似问题,第三段代码则解决了一个与坐标轴相关的问题,计算从两个点到坐标轴的斜率。
摘要由CSDN通过智能技术生成

#include<iostream>
using namespace std;
int g[129][129];
int main()
{
	int d;
	int n;
	cin >> d >> n;
	for (int i = 1;i <= n;i++)
	{
		int x, y, k;
		cin >> x >> y >> k;
		g[x][y] = k;
	}
	int max1 = -1;
	int max2 = -1;
	for (int i = 0;i <= 128;i++)
	{
		for (int j = 0;j <= 128;j++)
		{
			int sum = 0;
			for (int z = (i - d >= 0) ? (i - d) : 0;z <= (i + d <= 128 ? i + d : 128);z++)
			{
				for (int v = (j - d >= 0) ? (j - d) : 0;v <= (j + d <= 128 ? j + d : 128);v++)
				{
					sum += g[z][v];
				}
			}
			if (sum > max2)
			{
				max2 = sum;
				max1 = 1;
			}
			else if (sum == max2)
			{
				max1++;
			}
		}
	}
	cout << max1 << " " << max2 << endl;
	return 0;
}

//浅醉喝 做法 

#include<iostream>
using namespace std;
int g[130][130];
int s[130][130];
int d, n;
int main()
{
	cin >> d >> n;
	for (int i = 1;i <= n;i++)
	{
		int x, y, k;
		cin >> x >> y >> k;
		g[x+1][y+1] = k;
	}
	for (int i = 1;i <= 129;i++)
	{
		for (int j = 1;j <= 129;j++)
		{
			s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + g[i][j];
		}
	}
	//cout << s[7][7] - s[5-1][7] - s[7][5-1] + s[5-1][5-1] << "qwq";
	int max1 = -1;
	int max2 = -1;
	for (int i = 1;i <= 129;i++)
	{											
		for (int j = 1;j <= 129;j++)			
		{
			int sum = 0;
			int r1 = (i - d >= 1 ? i - d : 1);//上
			int r2 = (i + d <= 129 ? i + d : 129);//下
			int r3 = (j - d >= 1 ? j - d : 1);//左
			int r4 = (j + d <= 129 ? j + d : 129);//右
			sum = s[r2][r4] - s[r2][r3-1] - s[r1-1][r4] + s[r1-1][r3-1];
			if (sum > max2)
			{
				max2 = sum;
				max1 = 1;
			}
			else if(max2==sum)
			{
				max1++;
			}
		}
		
	}
	cout << max1 << " " << max2 << endl;
	return 0;
}

#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
char g[20][20];
int n;
int y[20];
int x[20];
int main()
{
	cin >> n;
	double rx, ry, lx, ly, ox, oy, flag = 0;
	for (int j = n;j >=1;j--)
	{
		for (int i = 1;i <= n;i++)
		{
			cin >> g[j][i];
			if (g[j][i] == '1')
			{
				x[i]++;
				y[j]++;
			}
			if (g[j][i] == 'x')
			{
				if (flag == 0)
				{
					rx = i;
					ry = j;
					flag = 1;
				}
				else
				{
					lx = i;
					ly = j;
				}
			}
		}
	}
	int xx = 0, yy = 0;
	for (int i = 1;i <=19;i++)
	{
		if (x[i] > xx)
		{
			xx = x[i];
			ox = i;
		}
		if (y[i] > yy)
		{
			yy = y[i];
			oy = i;
		}
	}
	int xxx = rx;
	int yyy = ry;
	int cnt = 0;
	if (g[yyy + 1][xxx] == '1')
	{
		cnt++;
	}
	if (g[yyy - 1][xxx] == '1')
	{
		cnt++;
	}
	if (g[yyy ][xxx-1] == '1')
	{
		cnt++;
	}
	if (g[yyy ][xxx+1] == '1')
	{
		cnt++;
	}
	if (cnt >= 3)
	{
		ox = rx;
		oy = ry;
	}
	 xxx = lx;
	 yyy = ly;
	 cnt = 0;
	if (g[yyy + 1][xxx] == '1')
	{
		cnt++;
	}
	if (g[yyy - 1][xxx] == '1')
	{
		cnt++;
	}
	if (g[yyy][xxx - 1] == '1')
	{
		cnt++;
	}
	if (g[yyy][xxx + 1] == '1')
	{
		cnt++;
	}
	if (cnt >= 3)
	{
		ox = lx;
		oy = ly;
	}
	//cout << lx << " " << ly << " " << rx << " " << ry << endl;
	if (ly == ry)
	{
		cout << "y=";
		cout << fixed << setprecision(4) << ly - oy << endl;
		return 0;
	}
	else if (lx == rx)
	{
		cout << "x=";
		cout << fixed << setprecision(4) << lx - ox << endl;
	}
	else
	{

		double k = (ly - ry) / (lx - rx);
		double b = (ry-oy) - (k * (rx-ox));
		cout << "y=";
		cout << fixed << setprecision(4) << k;
		cout << 'x';
		if (b != 0)
		{
			if (b > 0)
			{
				cout << "+";
			}
			cout << fixed << setprecision(4) << b << endl;
		}

	}
	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值