从左上角开始在无限螺旋矩阵中找到给定元素 N 的位置
给定一个数字N和一个无限二维矩阵,使用下面给出的算法填充,任务是找到矩阵中存在的给定元素的坐标。算法如下:
- 矩阵的最左边和最上面的单元格用 1 填充。然后,所有单元格都用从 2 开始的连续数字填充
- 第一行中最左边的未填充单元格已填充。然后,当最后一个填充单元格的左邻居被填充时,它下面的单元格接下来被填充,直到最后一个填充单元格有一个未填充的左邻居
- 接下来,从右到左填充单元格,直到第一列。然后,再次填充第一行中最左边的未填充单元格
- 以上两步无限重复
例子:
Input: N = 12
Output: 3 4
Explanation: 12 lies in the cell with row as 3 and column as 4
Input: N = 10549857
Output: 353 3249
Explanation: 10549857 lies in the cell with row as 353 and column as 3249
方法:可以进行以下观察:
- 在上图中,由第 3 行和第 3 列形成的红色突出部分或“倒 L”由所有大于 4 且小于 10 的数字组成。类似地,由第 4 行和第 4 列形成的“倒 L”包括大于 9 小于 17 的数字
- 换句话说,出现的数字不包括当前完美正方形,直到包括下一个完美正方形
- 要找到数字所在的“倒 L”,请找到数字平方根的 ceil
- 计算“倒 L”的平方与给定数字之间的差,例如d
- 如果l是数字所在的“倒数 L”,并且n表示给定数字,则:
d = l^2 – N
- 如果此差异d小于n ,则n的行r和列c由下式给出:
r = l
c = d + l
- 如果此差d大于或等于n ,则n的行r和列c由下式给出:
r = 2l – d – 1
c = l
下面是上述方法的实现:
C++
// C++ Implementation for the above approach
#include
using namespace std;
// Function to find the coordinates of n
void findCoordinates(int n)
{
// Store the row and column of
// n respectively
int r, c;
// Stores the inverted L
// in which n lies
int l = ceil(sqrt(n));
// Stores the difference between
// square of l and n
int d = (l * l) - n;
// If d is less than l
if (d < l) {
r = l;
c = d + 1;
}
else {
c = l;
r = (2 * l) - d - 1;
}
cout << r << " " << c;
}
// Driver code
int main()
{
// Given n
int N = 10549857;
// Function call
findCoordinates(N);
return 0;
}
Java
// Java Implementation for the above approach
public class GFG {
// Function to find the coordinates of n
static void findCoordinates(int n)
{
// Store the row and column of
// n respectively
int r, c;
// Stores the inverted L
// in which n lies
int l = (int)Math.ceil(Math.sqrt(n));
// Stores the difference between
// square of l and n
int d = (l * l) - n;
// If d is less than l
if (d < l) {
r = l;
c = d + 1;
}
else {
c = l;
r = (2 * l) - d - 1;
}
System.out.print(r + " " + c);
}
// Driver code
public static void main (String[] args) {
// Given n
int N = 10549857;
// Function call
findCoordinates(N);
}
}
// This code is contributed by AnkThon
Python3
# Python Program to implement
# the above approach
import math
# Function to find the coordinates of n
def findCoordinates(n):
# Store the row and column of
# n respectively
#let r, c
# Stores the inverted L
# in which n lies
l = math.ceil(math.sqrt(n))
# Stores the difference between
# square of l and n
d = (l * l) - n
# If d is less than l
if (d < l):
r = l
c = d + 1
else:
c = l
r = (2 * l) - d - 1
print(f"{r} {c}")
# Driver code
# Given n
N = 10549857
# Function call
findCoordinates(N)
# This code is contributed by Saurabh Jaiswal
C#
// C# Implementation for the above approach
using System;
public class GFG {
// Function to find the coordinates of n
static void findCoordinates(int n)
{
// Store the row and column of
// n respectively
int r, c;
// Stores the inverted L
// in which n lies
int l = (int)Math.Ceiling(Math.Sqrt(n));
// Stores the difference between
// square of l and n
int d = (l * l) - n;
// If d is less than l
if (d < l) {
r = l;
c = d + 1;
}
else {
c = l;
r = (2 * l) - d - 1;
}
Console.Write(r + " " + c);
}
// Driver code
public static void Main (string[] args) {
// Given n
int N = 10549857;
// Function call
findCoordinates(N);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
353 3249
时间复杂度: O(1)
辅助空间: O(1)