Converting numbers within grid to their corresponding x,y coordinates(将网格内的数字转换为其对应的 x,y 坐标)
问题描述
给定以下网格中的数字(从 1 到 36),我如何确定它们在网格中的坐标 (x,y)?
Given the numbers in the following grid (from 1 to 36), how can I determine their coordinates (x,y) within the grid?
 |  0  1  2  3  4  5  6  7  8
------------------------------
0|  1  2  3  4  5  6  7  8  9
1| 10 11 12 13 14 15 16 17 18
2| 19 20 21 22 23 24 25 26 27
3| 28 29 30 31 32 33 34 35 36
即我想要获得的是以下内容:
i.e. what I want to obtain is the following:
 |     0     1     2        8
------------------------------
0| (0,0) (1,0) (2,0) ... (8,0)
1| (0,1) (1,1) (2,1) ... (8,1)
2| (0,2) (1,2) (2,2) ... (8,2)
3| (0,3) (1,3) (2,3) ... (8,3)
我试过了:
x = number%9-1;
y = number/9;
适用于除右侧最后一列之外的所有情况.
which works for all cases except the ones in the last column on the right.
所以我想出了:
if (number%9==0) {
    x = 8;
    y = number/9-1;
}
else{
    x = number%9-1;
    y = number/9;
}
我的问题是,有没有更聪明的方法来做到这一点?
My question is, is there a smarter way of doing this?
推荐答案
x = (number-1)%9;
y = (number-1)/9;
                        这篇关于将网格内的数字转换为其对应的 x,y 坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将网格内的数字转换为其对应的 x,y 坐标
				
        
 
            
        基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 这个宏可以转换成函数吗? 2022-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 - 常量变量在标题中不起作用 2021-01-01
 - C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				