UVa 10182 Bee Maja (规律&O(1)算法)

本文深入探讨了Maja与Willi在不同坐标系统下的问题,并提供了将Willi的坐标转换为Maja坐标的具体算法实现,包括输入输出规范及O(1)复杂度的解法。

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

10182 - Bee Maja

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123

Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive consists of many hexagonal honey combs where the honey is stored in.
But bee Maja has a problem. Willi told her where she can meet him, but because Willi is a male drone and Maja is a female worker they have different coordinate systems.

Maja's Coordinate System
Willi's Coordinate System
Maja who often flies directly to a special honey comb has laid an advanced two dimensional grid over the whole hive.Willi who is more lazy and often walks around just numbered the cells clockwise starting from 1 in the middle of the hive.
MajaWilli

Help Maja to convert Willi's system to hers. Write a program which for a given honey comb number gives the coordinates in Maja's system.

Input Specification

The input file contains one or more integers which represent Willi's numbers. Each number stands on its own in a separate line, directly followed by a newline. The honey comb numbers are all less than 100 000.

Output Specification

You should output the corresponding Maja coordinates to Willi's numbers, each coordinate pair on a separate line.

Sample Input

1
2
3
4
5

Sample Output

0 0
0 1
-1 1
-1 0
0 -1

O(1)复杂度的思路:这道题如果只看左边那个图的话就会发现竖着的每一列纵坐标相同,而向左下方斜着的每一列纵坐标相同。但是这样并不能找出这些点的关系,其实换个角度,如果把左边的坐标在直角坐标系上画出来,就比较容易看出这是一个很有规律的一个图,只需要横纵坐标的加减就好了,至于加减多少那就看这个数所在的层数,层数可以根据x轴上的坐标的通项求出。


完整代码:

/*0.015s*/

#include<cstdio>
#include<cmath>

int main()
{
	int n, m, i, j;
	while (~scanf("%d", &m))
	{
		if (m == 1)
		{
			puts("0 0");
			continue;
		}
		--m;
		n = ceil((sqrt(12 * m + 9) - 3) / 6);
		if (3 * n * (n + 1) == m) printf("%d %d\n", n, 0);///x轴为起点
		else
		{
			--n;///前一个n
			m -= 3 * n * (n + 1);
			if (m <= n + 1) printf("%d %d\n", n - m + 1, m);
			else if (m <= (n + 1) << 1)
			{
				m -= (n + 1);
				printf("%d %d\n", -m, n + 1);
			}
			else if (m <= 3 * (n + 1))
			{
				m -= (n + 1) << 1;
				printf("%d %d\n", -(n + 1), n + 1 - m);
			}
			else if (m <= (n + 1) << 2)
			{
				m -= 3 * (n + 1);
				printf("%d %d\n", -(n - m + 1), -m);
			}
			else if (m <= 5 * (n + 1))
			{
				m -= (n + 1) << 2;
				printf("%d %d\n", m, -(n + 1));
			}
			else if (m <= 6 * (n + 1))
			{
				m -= 5 * (n + 1);
				printf("%d %d\n", n + 1, -(n + 1 - m));
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值