简体   繁体   中英

Figuring out a Row of a Tile number in Python

Say I have a grid that looks something like this:

0 1 2 3
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. Imagine someone else has stored the values w (for width) and h (for height), that they have read in from a text file. You have access to these stored values, as long as you call them w and 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.

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.

You just divide the tile number by the number of tiles in a row w : row = t//w

To get the column do: col = t%w

I assume both row and column start at zero. If not, just add 1 where you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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