简体   繁体   English

查找给定矩阵的元素

[英]Find elements of given matrix

You are given an infinite matrix whose upper-left square starts with 1. Here are the first five rows of the infinite matrix:给定一个无限矩阵,其左上角的平方以 1 开头。这里是无限矩阵的前五行:

 1  2  9 10 25
 4  3  8 11 24
 5  6  7 12 23
16 15 14 13 22
17 18 19 20 21

Your task is to find out the number in presents at row x and column y after observing a certain kind of patter present in the matrix你的任务是在观察到矩阵中存在的某种模式后找出 x 行和 y 列的存在数

Input Format The first input line contains an integer t: the number of test cases After this, there are t lines, each containing integer x and y输入格式 第一个输入行包含一个 integer t:测试用例的数量 在这之后有 t 行,每行包含 integer x 和 y

For each test, print the number present at xth row and yth column.对于每个测试,打印出现在第 x 行和第 y 列的数字。 sample input 3 2 3 1 1 4 2 sample output 8 1 15样品输入 3 2 3 1 1 4 2 样品 output 8 1 15

Hint: the numbers at the right and bottom border of a left upper square are consecutive (going either down and left, or right and up).提示:左上方块的右下边界的数字是连续的(向下和向左,或向右和向上)。 First determine in which border your position is, then find out which direction applies, and finally find the correct number at the position (which easy formula gives you the first number in the border?).首先确定你的 position 在哪个边界,然后找出适用的方向,最后在 position 找到正确的数字(哪个简单的公式给你边界的第一个数字?)。

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

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