Bắt đầu với một con trăn lưới hai chiều vô hạn

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 approach0

// C++ Implementation for the above approach1

// C++ Implementation for the above approach2____03

// C++ Implementation for the above approach2// C++ Implementation for the above approach5

// C++ Implementation for the above approach2int // C++ Implementation for the above approach8

 

// C++ Implementation for the above approach2#include 0

// C++ Implementation for the above approach2#include 2

// C++ Implementation for the above approach2______8 #include 5____16#include 7#include 8#include 9

 

// C++ Implementation for the above approach2____21

// C++ Implementation for the above approach2using3

// C++ Implementation for the above approach2int using6

 

// C++ Implementation for the above approach2using8

// C++ Implementation for the above approach2namespace0 namespace1

namespace2____33

namespace2____35

// C++ Implementation for the above approach2____37

// C++ Implementation for the above approach2namespace9 // C++ Implementation for the above approach1

namespace2____42

namespace2____44

// C++ Implementation for the above approach2____37

 

// C++ Implementation for the above approach2____48____49 // Function to find the coordinates of n0

namespace7

 

// Function to find the coordinates of n2

int // Function to find the coordinates of n4

// C++ Implementation for the above approach1

// C++ Implementation for the above approach2// Function to find the coordinates of n7

// C++ Implementation for the above approach2int void0

 

// C++ Implementation for the above approach2____62

// C++ Implementation for the above approach2____64

 

// C++ Implementation for the above approach2void6 void7

namespace7

Java




void9

 

findCoordinates[0 findCoordinates[1 findCoordinates[2

// C++ Implementation for the above approach2

// Function to find the coordinates of n

findCoordinates[5 void findCoordinates[__int // C++ Implementation for the above approach0

// C++ Implementation for the above approach1

int1

// C++ Implementation for the above approach2____03

// C++ Implementation for the above approach2// C++ Implementation for the above approach5

// C++ Implementation for the above approach2int // C++ Implementation for the above approach8

 

// C++ Implementation for the above approach2#include 0

// C++ Implementation for the above approach2#include 2

// C++ Implementation for the above approach2______8 // C++ Implementation for the above approach05____8// C++ Implementation for the above approach07

 

// C++ Implementation for the above approach2____21

// C++ Implementation for the above approach2using3

// C++ Implementation for the above approach2int using6

 

// C++ Implementation for the above approach2using8

// C++ Implementation for the above approach2namespace0 namespace1

namespace2____33

namespace2____023____024// C++ Implementation for the above approach25

// C++ Implementation for the above approach2____37

// C++ Implementation for the above approach2namespace9 // C++ Implementation for the above approach1

namespace2____42

namespace2______034// C++ Implementation for the above approach35 // C++ Implementation for the above approach36// C++ Implementation for the above approach24// C++ Implementation for the above approach25

// C++ Implementation for the above approach2____37

 

// C++ Implementation for the above approach2______042std;9 // C++ Implementation for the above approach44

namespace7

 

// C++ Implementation for the above approach2// Function to find the coordinates of n2

// C++ Implementation for the above approach2findCoordinates[0 findCoordinates[5 void // C++ Implementation for the above approach52

namespace2____57

namespace2____8 // C++ Implementation for the above approach57____058// C++ Implementation for the above approach25

// C++ Implementation for the above approach2

namespace2____62

namespace2____64

 

// C++ Implementation for the above approach2____37

namespace7

 

// C++ Implementation for the above approach68

Python3




// C++ Implementation for the above approach69

// C++ Implementation for the above approach70

// C++ Implementation for the above approach71 // C++ Implementation for the above approach72

 

// C++ Implementation for the above approach73

// C++ Implementation for the above approach74 // C++ Implementation for the above approach75

int1

// C++ Implementation for the above approach2____078

// C++ Implementation for the above approach2____080

// C++ Implementation for the above approach2____082

 

// C++ Implementation for the above approach2____084

// C++ Implementation for the above approach2____086

// C++ Implementation for the above approach2____088// C++ Implementation for the above approach89 // C++ Implementation for the above approach90

 

// C++ Implementation for the above approach2____092

// C++ Implementation for the above approach2// C++ Implementation for the above approach94

// C++ Implementation for the above approach2______096// C++ Implementation for the above approach89 // C++ Implementation for the above approach98// C++ Implementation for the above approach99 #include 00#include 01 #include 02

 

// C++ Implementation for the above approach2#include 04

// C++ Implementation for the above approach2____30 #include 07

namespace2______109// C++ Implementation for the above approach89 #include 7// C++ Implementation for the above approach35 // C++ Implementation for the above approach99 #include 00#include 01 // C++ Implementation for the above approach96#include 01 // C++ Implementation for the above approach24

Chủ Đề