1438C - Engineer Artem (构造)

题目

思路:如横纵坐标相加为奇数则将对应位置的数变为奇数+1或不加,如横纵坐标相加为偶则将对应位置数变为偶数。

这题我觉得要培养一种感觉,看到一个题目,它给了什么条件要按给的条件那个方向去多思考一下,就像本题说可以加一或不加,那么我们想想加一其实比较容易想到的就是改变奇偶性。而有些题它给了一些数据大小,其实也是一种引导,引导着我们要去用符合数据范围的算法,从出题人的角度想想这些条件可能要考什么。

#include<iostream>
using namespace std;

int main()
{
    
    
	int a, b, t;cin >> t;
	while (t--)
	{
    
    
		int n, m;
		cin >> n >> m;
		for (int i = 1;i <= n;i++)
			for (int j = 1;j <= m;j++)
			{
    
    
				cin >> a;
				if ((a % 2 && (i + j) % 2) || (a % 2 == 0 && (i + j) % 2 == 0)) cout << a << " ";
				else cout << a + 1 << " ";
			}
		cout << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/asbbv/article/details/113873467