Hướng dẫn diagonal python - trăn chéo
Anti-Diagonals Show
Nội phân Chính showShow
Đ-c ĐA Đầu vào Dengng Đầu vào đầu tin sẽ chứa các giá trị m, n Được phân tách bằng không gian. Dngng thứ Hai sẽ chứa ma trận a Có kích thước mxn. ĐầU ra Đầu ra phải chứa cau phần tử chống Đường Chéo
Tìm hiểu thênm về sự Giúp Đỡ của chún tôms Anti-Diagonals Đ-c ĐA Đầu vào Dengng Đầu vào đầu tin sẽ chứa các giá trị m, n Được phân tách bằng không gian. Dngng thứ Hai sẽ chứa ma trận a Có kích thước mxn. ĐầU ra Đầu ra phải chứa cau phần tử chống Đường Chéo Giải trình Ví dụ: nếu m = 4, n = 4 Ma trận A: 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Vì vậy, đầu ra phải 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16 Đầu vào mẫu 1 2 3 1 5 5 2 7 8 Đầu ra mẫu 1 1 5 2 5 7 8 Đầu vào mẫu 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 5 3 6 9 4 7 10 13 8 11 14 12
Xem thảo luận Cải thiện bài viết Lưu bài viết Xem thảo luận Cải thiện bài viết Lưu bài viết ĐọcN*N, return an array of its anti-diagonals. For better understanding let us look at the image given below: Examples: Input : Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16 Bàn luận
Cái đầu tiên là, một số đường chéo bắt đầu từ hàng zeroth cho mỗi cột và kết thúc khi bắt đầu cột> = 0 hoặc hàng bắt đầu C++ 17 18 19 20
Dưới đây là việc thực hiện phương pháp trên: & nbsp; 4 18 19 20 0 1 2 3 5 2 7 5 9 Input :0 2 Input :2 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 160 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 Input :80 Input :81 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 2 Input :5 Input :3 Input :7 Input :8 5 9 Input :0 2 Input :89 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 160 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 Input :80 Input :81 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 2 22Input :3 Input :7 25 4Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 5 190 2 184Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)5def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)2 using8 5 192 193Java 5 4 195 196 5
0 1 2 201Input :3 2 206 207Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 Input :3 9 Input :0 2 03 044____105 16 17 18 19Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 29Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 2 08 04Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 Input :9 Input :7 13 04 15Input :3 9 Input :0 2 36 37 38 16 17 18 19Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 16
Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 29Input :9 2 41 37Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 Input :7 46 04 48 5 4Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 5 68 69 ____10 71Input :3 98 99 Input :00 Input :3 Input :02 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Python3Input :06 Input :07 5Input :09 Input :10 207 5 9 Input :14 Input :15 Input :16 Input :17 Input :3 Input :19 Input :10 Input :21 Input :3 Input :23 Input :10 04Input :3 Input :7 Input :28 Input :10 04 Input :31 Input :32 Input :33 Input :9 Input :35 Input :36 Input :10 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 Input :39 Input :9 Input :19 Input :42 Input :10 37Input :9 Input :23 Input :47 Input :10 37Input :3 Input :35 Input :52 5 9 Input :55 Input :15 Input :16 Input :0 37Input :60 Input :3 Input :23 Input :10 Input :64 Input :3 Input :19 Input :10 Input :09 Input :42 37Input :3 Input :7 Input :73 Input :31 Input :32 Input :76 Input :10 04Input :79 Input :9 Input :35 Input :82 Input :83 Input :84 Input :10 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 Input :39 Input :9 Input :19 Input :42 Input :10 37Input :9 Input :23 Input :47 Input :10 37Input :3 Input :35 Input :52 5 9 Input :55 Input :15 Input :16 Input :0 37Input :60 Input :3 Input :23 Input :10 Input :64 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1617 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1618 85 80 87 80 89Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1616 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1617 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1618 91 80 93 80 95Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1632 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1634 C#Input :3 Input :19 Input :10 Input :09 Input :42 37Input :32 18Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1636 195Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1638 5 69 0 1 2Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1644 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1662 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1672 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 2 7Input :3 9 Input :0 2 Input :2 Input :9 2 Input :5 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1662 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 16Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1672 Input :9 Input :7 Input :8 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 9 Input :0 2 Input :89 5 4Input :3 2Input :813 Input :9 Input :815 Input :3 190 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Đầu vào: 9DEF Antidiagonals (M, N, Ma trận): & nbsp; & nbsp; & nbsp; "" "In các chất chống đối một trên mỗi dòng ma trận MXN được đại diện & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; , 5, 2, 7, 8]) & nbsp; & nbsp; & nbsp; 1 & nbsp; & nbsp; (3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) & nbsp; 3 6 9 & nbsp; & nbsp; & nbsp; 4 7 10 & nbsp; antidiagonals = [[] cho _ trong phạm vi (m+n-1)] & nbsp; & nbsp; & nbsp; cho i trong phạm vi (m*n): & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; adindex = (i // n) + (i % n) & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; Antidiagonals [adindex] .Append (ma trận [i]) & nbsp; & nbsp; & nbsp; cho d trong Antidiagonals: & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; print (*d) # mã trình điều khiển m, n = list (map (int, input ('m n:') .split ())) ; & nbsp; A += map (int, input ('a [ % d]:' % i) .split ()) antidiagonals (m, n, a) nếu __name__ == '__main__': & nbsp; Nhập tài liệu & nbsp; doctest.testmod (verbose = true) 2 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162Input :821 Input :9 Input :7 25 4 5Input :826 5 9Input :829 5 4Input :3 Input :833 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 4 5 68 69 0Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 Input :850 Input :851 21 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 5 9Input :857 5 4Input :3 Input :861 JavaScript Input :3 4Input :9 Input :840 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 164 Input :9 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 166 Input :3 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :3 Input :850 Input :851 21 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :884 190Input :886 Output: Input :8 4022 ________ 334O (n*n), trong đó n là số lượng hàng hoặc cột của ma trận đã cho22 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1634O(N*N), Where N is the number of rows or columns of given matrix. Auxiliary Space:O(1) 423Input :7 Input :8363 Input :7 Input :836 Input :9 Input :840 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 i (hàng) an j (cột) .Input :27 = N-1). & Nbsp; i (row) an j (column). Input :3 Input :7 Input :864N*N matrix, we will observe that the sum of indices for any element lies between 0 (when i = j = 0) and 2*N – 2 (when i = j = N-1). Độ phức tạp về thời gian: O (n*n), trong đó n là số lượng hàng hoặc cột của ma trận đã cho.AUXILIARY: O (1)
Nếu chúng ta tìm thấy tổng số chỉ số của bất kỳ phần tử nào trong & nbsp; ma trận n*n, chúng ta sẽ quan sát rằng tổng số chỉ số cho bất kỳ phần tử nào nằm giữa 0 (khi i = j = 0) và 2*n - 2 (khi tôi = j = n-1). & nbsp; C++ 17Input :888 Vì vậy, chúng tôi sẽ làm theo các bước sau: & nbsp; Khai báo một vectơ của các vectơ có kích thước 2*n - 1 để giữ các khoản tiền duy nhất từ sum = 0 đến sum = 2*n - 2. 4Bây giờ chúng ta sẽ lặp qua vectơ và đẩy lùi các phần tử tương tự vào cùng một hàng trong vectơ của vectơ. Dưới đây là việc thực hiện phương pháp trên: & nbsp; 18 19 20 0 Input :893 2Input :895 5 2 Input :899 Input :9 218 5 2 202 5 4Input :3 227 5 204__12Input :9 234Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 2 184 4 5 204 2 246 247 248 247 250 247 252 5 190 5 192 193Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Java 259 260 259 262 195 264 69 ____10 1 2 269 4 5 2 273 5 2 206 81 278 37Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5 282 99 284 5 9Input :0 2 289 04 291Input :3 293 99 295Các Các Input :9 1711 5 9Input :0 2 289 04 1718 5 4Input :3 29Các Input :9 1731Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 161 21 5Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 68 69 0 1740 4Các Các Các Các 5 190Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Python3Input :9 1711 5 9Input :0 2 289 04 1718 5 4Input :3 29Các Các Các Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1634 C# 18Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 1636 Input :9 1711 5 9Input :0 2 289 04 1718 5 4Input :3 29 195 264 69 ____10 1 2 269 4 5 2 273 5 2 206 81 278 37Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 162 5 282 99 284 5 9Input :0 2 289 04 291Input :3 293 99 295 1974Các 1974Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 168 Input :9def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)1711def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)5def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)9Input :0def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)2 def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)289def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)04def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)1718def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)5def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)4Input :3def antidiagonals(M, N, matrix): """Prints antidiagonals one per line of MxN matrix represented by 1-dimantional array >>> antidiagonals(2, 3, [1, 5, 5, 2, 7, 8]) 1 5 2 5 7 8 >>> antidiagonals(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) 1 2 5 3 6 9 4 7 10 8 11 12 """ antidiagonals = [[] for _ in range(M+N-1)] for i in range(M*N): adindex = (i // N) + (i % N) antidiagonals[adindex].append(matrix[i]) for d in antidiagonals: print(*d) # Driver code M, N = list(map(int, input('M N: ').split())) A = [] for i in range(1, M+1): A += map(int, input('A[%d]: ' % i).split()) antidiagonals(M, N, A) if __name__ == '__main__': import doctest doctest.testmod(verbose=True)29CácĐầu ra: & nbsp; 2 Độ phức tạp về thời gian: O (n*n), trong đó n là số lượng hàng hoặc cột của ma trận đã cho. O(N*N), Where N is the number of rows or columns of given matrix.Auxiliary Space:O(N*N) O(N*N), Where N is the number of rows or columns of given matrix. Làm thế nào để bạn tìm thấy AntiCó thể thu được chất chống diaka bằng cách đảo ngược thứ tự của các phần tử bằng cách sử dụng numpy.flipud hoặc numpy.fliplr.reversing the order of elements using either numpy.flipud or numpy.fliplr .reversing the order of elements using either numpy. flipud or numpy. fliplr . Anti là gìTrong toán học, một ma trận chống đường chéo là một ma trận vuông trong đó tất cả các mục đều bằng không, ngoại trừ các mục trên đường chéo đi từ góc dưới bên trái sang góc trên bên phải (↗)Đường chéo, đường chéo theo đường chéo, đường chéo hoặc đường chéo xấu).a square matrix where all the entries are zero except those on the diagonal going from the lower left corner to the upper right corner (↗), known as the anti-diagonal (sometimes Harrison diagonal, secondary diagonal, trailing diagonal, minor diagonal, or bad diagonal).a square matrix where all the entries are zero except those on the diagonal going from the lower left corner to the upper right corner (↗), known as the anti-diagonal (sometimes Harrison diagonal, secondary diagonal, trailing diagonal, minor diagonal, or bad diagonal). Làm thế nào để bạn tìm thấy hình vuông hoàn hảo đầu tiên trong Python?Chương trình Python để kiểm tra xem một số là hình vuông hoàn hảo... Bước 1: Lấy đầu vào từ người dùng .. Bước 2: Tính toán căn bậc hai của số đã cho bằng thư viện toán học .. Bước 3: Kiểm tra xem int (root + 0,5) ** 2 == Số, nếu điều này đánh giá là đúng thì số là một hình vuông hoàn hảo .. |