简体   繁体   English

在Python中找出平铺数字的行

[英]Figuring out a Row of a Tile number in Python

Say I have a grid that looks something like this: 假设我有一个看起来像这样的网格:

0 1 2 3 0 1 2 3
4 5 6 7 4 5 6 7

Imagine that we have aw by h grid, where the tiles are numbered starting at 1 in the top left corner. 想象一下,我们有一个由a乘h的网格,其中图块的编号从左上角的1​​开始。 Imagine someone else has stored the values w (for width) and h (for height), that they have read in from a text file. 想象其他人已经存储了他们从文本文件中读取的值w(对于宽度)和h(对于高度)。 You have access to these stored values, as long as you call them w and h. 只要将它们称为w和h,就可以访问这些存储的值。 Write a program to return: the row number of a tile number given by the user. 编写一个程序以返回:用户给定的瓦片号的行号。 Use integer division, //, which returns only a whole number and truncates any remainder. 使用整数除法///,它仅返回整数并舍去任何余数。 Start counting rows at row 0. 从第0行开始计数。

sum = ((t - 1) // h) + 1 answers the question if the tiles start at 1, but I can't figure it out when the tiles start at 0. sum = ((t - 1) // h) + 1回答了图块是否从1开始的问题,但是当图块从0开始时我无法弄清楚。

You just divide the tile number by the number of tiles in a row w : row = t//w 您只需将图块编号除以w的图块数量即可: row = t//w

To get the column do: col = t%w 要获取列,请执行以下操作: col = t%w

I assume both row and column start at zero. 我假设行和列都从零开始。 If not, just add 1 where you need. 如果不是,则在需要的地方添加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM