Codeforces Round #777 (Div. 2)A-C

A

解题思路:
1:1
2:2
3:21
4:121
5:212
6:2121
7:12121
8:21212
9:212121
n:………………
规律:只能用1,2组成数字n,一组1,2组成数字3。因此计算数字n对3求余。
一: 当n%3=0,说明组成n的1和2的数量相等。从2开始才能得到最大值。
二: 当n%3=1,说明组成n的数字1的数量>2。为了避免相同数字在一起,只能从1开始。
三: 当n%2=2,说明组成n的数字2的数量>1。为了避免相同数字在一起,只能从2开始。

#include<iostream>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int f = (n % 3 == 1 ? 1 : 2);//确定从哪个数字开始输出
		while (n > 0)
		{
			cout << f;
			n -= f;
			f = (f == 1 ? 2 : 1);//通过改变f交替输出即可
		}
		cout << endl;
	}
	return 0;
}

B

解题思路: 观察图形,会发现重叠的地方会形成在(x,y),(x+1,y),(x,y+1),(x+1,y+1)四点组成的方格只有三个黑色方块的情况。因此遍历方格,判断有无这种情况出现即可。

#include<iostream>
#include<string>
using namespace std;
string s[105];
 
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			cin >> s[i];
		int flag = 0;
		for (int i = 0; i < n - 1; i++)
		{
			for (int j = 0; j < m - 1; j++)
			{
				if (s[i][j] - '0' + s[i + 1][j] - '0' + s[i][j + 1] - '0' + s[i + 1][j + 1] - '0' == 3)
				{
					flag = 1;
					break;
				}
			}
			if (flag)
			{
				cout << "NO" << endl;
				break;
			}
		}
		if (flag == 0)
			cout << "YES" << endl;
	}
	return 0;
}

C

解法一:
对每个出现黑色格子的地方用12或21的矩形操作即可。操作步数即黑色格子的总数。当然第一行第一列要出现黑色格子是不可能的情况。直接判断输出-1即可。其他情况从最后一行最后一列的格子开始遍历即可。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string num[105];
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m, ans = 0;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
		{
			cin >> num[i];
			ans += count(num[i].begin(), num[i].end(), '1');
		}
		if (num[0][0] == '1')
		{
			cout << -1 << endl;
			continue;
		}
		cout << ans << endl;
		//当出现黑色格子时先用1*2的矩阵横向向前填充。如(i,j)是黑色格子,填充(i,j-1)-(i,j)的矩阵
		for (int i = n - 1; i >= 0; i--)
		{
			for (int j = m - 1; j > 0; j--)
			{
				if (num[i][j] == '1')
				{
					cout << i + 1 << ' ' << j << ' ' << i + 1 << ' ' << j + 1 << endl;
				}
			}
		}
		//第一列需要特殊处理。(因为不能在向前填充了,只能向上填充)
		for (int i = n - 1; i > 0; i--)
			if (num[i][0] == '1')
				cout << i << ' ' << 1 << ' ' << i + 1 << ' ' << 1 << endl;
	}
	return 0;
}

解法二
遍历每个点,若该点不为0(白色格子)则输出1*1的矩阵–即一个空白格。若该点为1(黑色格子)则先横向向前填充矩阵。同样第一列需要单独处理。总步数便是处理的总操作数。

#include<iostream>
#include<string>
using namespace std;
string num[105];

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			cin >> num[i];
		if (num[0][0] == '1')
		{
			cout << -1 << endl;
			continue;
		}
		cout << m * n - 1 << endl;//计算总的步数(n*(m-1)+n-1)= m*n-1
		for (int i = n - 1; i >= 0; i--)
			for (int j = m - 1; j > 0; j--)
			{
				if (num[i][j] == '1')
					cout << i + 1 << ' ' << j << ' ' << i + 1 << ' ' << j + 1 << endl;
				else
					cout << i + 1 << ' ' << j << ' ' << i + 1 << ' ' << j << endl;
			}
		for (int i = n - 1; i > 0; i--)
		{
			if (num[i][0] == '1')
				cout << i << ' ' << 1 << ' ' << i+1 << ' ' << 1 << endl;
			else
				cout << i << ' ' << 1 << ' ' << i << ' ' << 1 << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值