Cho một số N và một ma trận 2D vô hạn, được điền bằng thuật toán dưới đây, nhiệm vụ là tìm tọa độ của phần tử đã cho có trong ma trận. Thuật toán như sau
- Ô ngoài cùng bên trái và trên cùng của ma trận được lấp đầy bằng 1. Sau đó, tất cả các ô được điền bằng các số liên tiếp bắt đầu từ 2
- Ô không được điền ngoài cùng bên trái trong hàng đầu tiên được điền. Sau đó, trong khi hàng xóm bên trái của ô được lấp đầy cuối cùng được lấp đầy, các ô bên dưới nó được lấp đầy tiếp theo, cho đến khi ô được lấp đầy cuối cùng có một hàng xóm bên trái không được lấp đầy
- Tiếp theo, các ô được điền từ phải sang trái cho đến cột đầu tiên. Sau đó, một lần nữa ô không được điền ngoài cùng bên trái trong hàng đầu tiên được điền
- Hai bước trên được lặp đi lặp lại vô hạn
ví dụ
Đầu vào. N = 12
Đầu ra. 3 4
Giải thích. 12 nằm trong ô có hàng là 3 và cột là 4Đầu vào. N = 10549857
Đầu ra. 353 3249
Giải thích. 10549857 nằm trong ô có hàng là 353 và cột là 3249
Tiếp cận. Các quan sát sau đây có thể được thực hiện
- Trong hình trên, phần được đánh dấu màu đỏ hoặc 'L đảo ngược' được tạo bởi hàng thứ 3 và cột thứ 3 bao gồm tất cả các số lớn hơn 4 và nhỏ hơn 10. Tương tự, chữ 'L ngược' được tạo bởi hàng thứ 4 và cột thứ 4 bao gồm các số lớn hơn 9 và nhỏ hơn 17
- Nói cách khác, các số hiện tại không bao gồm hình vuông hoàn hảo hiện tại cho đến khi bao gồm hình vuông hoàn hảo tiếp theo
- Để tìm 'L' đảo ngược mà số đó nằm trong đó, hãy tìm giá trị trần của căn bậc hai của số đó
- Sự khác biệt giữa bình phương của 'L đảo ngược' và số đã cho được tính toán, giả sử d
- Nếu l là 'L đảo ngược' trong đó số nằm và n biểu thị số đã cho, thì
d = l^2 – N
- Nếu sự khác biệt d này nhỏ hơn n, thì hàng r và cột c của n được cho bởi
r = l
c = d + l
- Nếu hiệu d này lớn hơn hoặc bằng n thì hàng r và cột c của n cho bởi
r = 2l – d – 1
c = l
Dưới đây là việc thực hiện các phương pháp trên
C++
// C++ Implementation for the above approach
#include
using
namespace
std;
// Function to find the coordinates of n
void
findCoordinates[
int
// C++ Implementation for the above approach
0
// C++ Implementation for the above approach
1
// C++ Implementation for the above approach
2____03
// C++ Implementation for the above approach
2// C++ Implementation for the above approach
5
// C++ Implementation for the above approach
2int
// C++ Implementation for the above approach
8
// C++ Implementation for the above approach
2#include
0
// C++ Implementation for the above approach
2#include
2
// C++ Implementation for the above approach
2______8 #include
5____16#include
7#include
8#include
9
// C++ Implementation for the above approach
2____21
// C++ Implementation for the above approach
2using
3
// C++ Implementation for the above approach
2int
using
6
// C++ Implementation for the above approach
2using
8
// C++ Implementation for the above approach
2namespace
0 namespace
1
namespace
2____33
namespace
2____35
// C++ Implementation for the above approach
2____37
// C++ Implementation for the above approach
2namespace
9 // C++ Implementation for the above approach
1
namespace
2____42
namespace
2____44
// C++ Implementation for the above approach
2____37
// C++ Implementation for the above approach
2____48____49 // Function to find the coordinates of n
0
namespace
7
// Function to find the coordinates of n
2
int
// Function to find the coordinates of n
4
// C++ Implementation for the above approach
1
// C++ Implementation for the above approach
2// Function to find the coordinates of n
7
// C++ Implementation for the above approach
2int
void
0
// C++ Implementation for the above approach
2____62
// C++ Implementation for the above approach
2____64
// C++ Implementation for the above approach
2void
6 void
7
namespace
7
Java
void
9
findCoordinates[
0 findCoordinates[
1 findCoordinates[
2
// C++ Implementation for the above approach
2
// Function to find the coordinates of n
findCoordinates[
5 void
findCoordinates[
__int
// C++ Implementation for the above approach
0
// C++ Implementation for the above approach
1
int
1
// C++ Implementation for the above approach
2____03
// C++ Implementation for the above approach
2// C++ Implementation for the above approach
5
// C++ Implementation for the above approach
2int
// C++ Implementation for the above approach
8
// C++ Implementation for the above approach
2#include
0
// C++ Implementation for the above approach
2#include
2
// C++ Implementation for the above approach
2______8 // C++ Implementation for the above approach
05____8// C++ Implementation for the above approach
07
// C++ Implementation for the above approach
2____21
// C++ Implementation for the above approach
2using
3
// C++ Implementation for the above approach
2int
using
6
// C++ Implementation for the above approach
2using
8
// C++ Implementation for the above approach
2namespace
0 namespace
1
namespace
2____33
namespace
2____023____024// C++ Implementation for the above approach
25
// C++ Implementation for the above approach
2____37
// C++ Implementation for the above approach
2namespace
9 // C++ Implementation for the above approach
1
namespace
2____42
namespace
2______034// C++ Implementation for the above approach
35 // C++ Implementation for the above approach
36// C++ Implementation for the above approach
24// C++ Implementation for the above approach
25
// C++ Implementation for the above approach
2____37
// C++ Implementation for the above approach
2______042std;
9 // C++ Implementation for the above approach
44
namespace
7
// C++ Implementation for the above approach
2// Function to find the coordinates of n
2
// C++ Implementation for the above approach
2findCoordinates[
0 findCoordinates[
5 void
// C++ Implementation for the above approach
52
namespace
2____57
namespace
2____8 // C++ Implementation for the above approach
57____058// C++ Implementation for the above approach
25
// C++ Implementation for the above approach
2
namespace
2____62
namespace
2____64
// C++ Implementation for the above approach
2____37
namespace
7
// C++ Implementation for the above approach
68
Python3
// C++ Implementation for the above approach
69
// C++ Implementation for the above approach
70
// C++ Implementation for the above approach
71 // C++ Implementation for the above approach
72
// C++ Implementation for the above approach
73
// C++ Implementation for the above approach
74 // C++ Implementation for the above approach
75
int
1
// C++ Implementation for the above approach
2____078
// C++ Implementation for the above approach
2____080
// C++ Implementation for the above approach
2____082
// C++ Implementation for the above approach
2____084
// C++ Implementation for the above approach
2____086
// C++ Implementation for the above approach
2____088// C++ Implementation for the above approach
89 // C++ Implementation for the above approach
90
// C++ Implementation for the above approach
2____092
// C++ Implementation for the above approach
2// C++ Implementation for the above approach
94
// C++ Implementation for the above approach
2______096// C++ Implementation for the above approach
89 // C++ Implementation for the above approach
98// C++ Implementation for the above approach
99 #include
00#include
01 #include
02
// C++ Implementation for the above approach
2#include
04
// C++ Implementation for the above approach
2____30 #include
07
namespace
2______109// C++ Implementation for the above approach
89 #include
7// C++ Implementation for the above approach
35 // C++ Implementation for the above approach
99 #include
00#include
01 // C++ Implementation for the above approach
96#include
01 // C++ Implementation for the above approach
24