基于给定模式使用给定数字 N 的数字构造方阵
给定一个整数N ,任务是构造一个大小为M x M的矩阵mat[][] ('M' 是给定整数中的位数),使得矩阵的每个对角线l 包含相同的数字,放置根据给定整数中数字的位置,然后再次从后面重复这些步骤。
例子:
Input: N = 123
Output: {{1, 2, 3},
{2, 3, 2},
{3, 2, 1}}
Explanation: The desired matrix must be of size 3*3. The digits of N are 1, 2, and 3. Placing 1, 2 and 3 along the diagonals from the top left cell till the Nth diagonal, and 2, 1 just after the Nth diagonal till the bottom-most cell.
Input: N = 3219
Output: {{3, 2, 1, 9}, {2, 1, 9, 1}, {1, 9, 1, 2}, {9, 1, 2, 3}}
方法:可以通过以对角方式遍历矩阵并根据给定数字中的相应数字分配单元格值来解决该任务。
- 提取给定整数的数字并将其存储在向量中,例如v 。
- 再次以相反的顺序存储矩阵的第二半对角线的数字。
- 按所需顺序分配数字。
- 打印矩阵。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct the matrix
void constructMatrix(int n)
{
// Vector to store the
// digits of the integer
vector v;
// Extracting the digits
// from the integer
while (n > 0) {
v.push_back(n % 10);
n = n / 10;
}
// Reverse the vector
reverse(v.begin(), v.end());
// Size of the vector
int N = v.size();
// Loop to store the digits in
// reverse order in the same vector
for (int i = N - 2; i >= 0; i--) {
v.push_back(v[i]);
}
// Matrix to be constructed
int mat[N][N];
// Assign the digits and
// print the desired matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mat[i][j] = v[i + j];
cout << mat[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
int n = 3219;
// Passing n to constructMatrix function
constructMatrix(n);
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Collections;
class GFG {
// Function to construct the matrix
public static void constructMatrix(int n)
{
// Vector to store the
// digits of the integer
ArrayList v = new ArrayList();
// Extracting the digits
// from the integer
while (n > 0) {
v.add(n % 10);
n = n / 10;
}
// Reverse the vector
Collections.reverse(v);
// Size of the vector
int N = v.size();
// Loop to store the digits in
// reverse order in the same vector
for (int i = N - 2; i >= 0; i--) {
v.add(v.get(i));
}
// Matrix to be constructed
int[][] mat = new int[N][N];
// Assign the digits and
// print the desired matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mat[i][j] = v.get(i + j);
System.out.print(mat[i][j] + " ");
}
System.out.println("");
}
}
// Driver Code
public static void main(String args[]) {
int n = 3219;
// Passing n to constructMatrix function
constructMatrix(n);
}
}
// This code is contributed by gfgking.
Python3
# python program for the above approach
# Function to construct the matrix
def constructMatrix(n):
# Vector to store the
# digits of the integer
v = []
# Extracting the digits
# from the integer
while (n > 0):
v.append(n % 10)
n = n // 10
# Reverse the vector
v.reverse()
# Size of the vector
N = len(v)
# Loop to store the digits in
# reverse order in the same vector
for i in range(N-2, -1, -1):
v.append(v[i])
# Matrix to be constructed
mat = [[0 for _ in range(N)] for _ in range(N)]
# Assign the digits and
# print the desired matrix
for i in range(0, N):
for j in range(0, N):
mat[i][j] = v[i + j]
print(mat[i][j], end=" ")
print()
# Driver Code
if __name__ == "__main__":
n = 3219
# Passing n to constructMatrix function
constructMatrix(n)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to construct the matrix
static void constructMatrix(int n)
{
// Vector to store the
// digits of the integer
List v = new List();
// Extracting the digits
// from the integer
while (n > 0) {
v.Add(n % 10);
n = n / 10;
}
// Reverse the vector
v.Reverse();
// Size of the vector
int N = v.Count;
// Loop to store the digits in
// reverse order in the same vector
for (int i = N - 2; i >= 0; i--) {
v.Add(v[i]);
}
// Matrix to be constructed
int[,] mat = new int[N, N];
// Assign the digits and
// print the desired matrix
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mat[i, j] = v[i + j];
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
int n = 3219;
// Passing n to constructMatrix function
constructMatrix(n);
}
}
// This code is contributed by sanjoy_62.
Javascript
输出:
3 2 1 9
2 1 9 1
1 9 1 2
9 1 2 3
时间复杂度: O(N 2 )
辅助空间: O(1)