【发布时间】:2019-01-13 20:13:35
【问题描述】:
所以我有一个网格,它是一种形式,我试图获取 X 和 Y。
是否有一个公式可以让我将例如 12 转换为 2,2 或 14 转换为 2,3
这种类型的网格也有名称吗?
static int getX(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==0)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
static int getY(int z)
{
int count = 0;
int res = 0;
int curr = 0;
for(int temp = z; temp > 0; temp >>= 1)
{
if(count % 2 ==1)
{
res += ((temp & 1) << curr);
curr++;
}
count++;
}
return res;
}
【问题讨论】:
-
如果找不到模式,可以使用字典/关联数组将每个输入数字(例如 12)映射到输出坐标(例如 (2,2)) .
-
这看起来像一条“Z 阶曲线”。
-
@Sneftel 看起来是这样 :) 我会用谷歌搜索一下。
标签: math