The question shows a snake matrix, which requires that the given value be calculated at the position of the snake matrix.
Note that the value in the upper right corner of the matrix is the square of an odd number, which is 1, 3, 5... . First, obtain the coordinates of the number in the upper right corner of the required number, and then calculate the coordinates of the specified number based on the distance.
#include
# include
using namespace STD; int n, m; void find () {int place_zero = n/2 + 1; int Len = 1, right_up = 1; int S = 1; // calculate the value while (right_up
= 0 & CNT
= Len & CNT <= 2 * (len-1) // left {Y = Y-(LEN-1 ); X = x + CNT-len + 1;} else if (CNT> 2 * (len-1) & CNT <= 3*(len-1 )) // The following {x = x + len-1; y = Y-(LEN-1) + CNT-2 * (len-1 );} elsex = x + len-1-(CNT-3 * (len-1); // right printf ("% d \ n", x, y );} int main () {INT cases; scanf ("% d", & cases); While (cases --) {scanf ("% d", & N, & M); find () ;}return 0 ;}