找规律-CodeForces 1027B-Numbers on the Chessboard

博客围绕CodeForces 1027B的Numbers on the Chessboard题目展开,这是一个填数问题,坐标x+y为偶数时填1~n*n/2的数,其他填在x+y为奇数处。题解考虑矩阵阶的奇偶,分别给出不同情况下根据坐标求数值的公式,并提及代码但未展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 找规律-CodeForces 1027B-Numbers on the Chessboard


  • 题目链接:

B. Numbers on the Chessboard

  • 思路:

题目大意:

填数问题,坐标(1开始)x+y为偶数的填 1~n*n/2 的数,其他数填在x+y为奇数的各种,给定坐标,求数值

         

题解:

考虑矩阵阶的奇偶

n%2==0:

x+y为偶数:num=(x-1)*(n/2)+((y+1)/2 )

x+y为奇数:num+n*n/2

n%2==1:

x+y为偶数:num=((x-1)*n+y+1)/2

x+y为奇数:num+n*n/2+1

  • 代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
 
int main()
{
	ll n,m,i;
	cin>>n>>m;
	while(m--)
	{
		ll x,y,num;
		cin>>x>>y;
		if(n%2==0)
		{
			num=(x-1)*(n/2)+((y+1)/2);
			if((x+y)%2==0)
				cout<<num<<endl;
			else
				cout<<num+n*n/2<<endl;
		}
		else
		{
			num=((x-1)*n+y+1)/2;
			if((x+y)%2==0)
				cout<<num<<endl;
			else
				cout<<num+n*n/2+1<<endl;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值