给定整数N ,任务是使用范围[1,N]的正整数生成尺寸为N x N的矩阵,以使次对角线的总和为理想正方形。
例子:
Input: N = 3
Output:
1 2 3
2 3 1
3 2 1
Explanation:
The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32).
Input: N = 7
Output:
1 2 3 4 5 6 7
2 3 4 5 6 7 1
3 4 5 6 7 1 2
4 5 6 7 1 2 3
5 6 7 1 2 3 4
6 7 1 2 3 4 5
7 1 2 3 4 5 6
Explanation:
The sum of secondary diagonal = 7 + 7 + 7 + 7 + 7 + 7 + 7 = 49(= 72).
方法:由于生成的矩阵的尺寸必须为N x N ,因此,为了使辅助对角线中的元素之和成为一个完美的正方形,我们的想法是在辅助对角线的每个索引处分配N。因此,该对角线中所有N个元素的总和为N 2 ,这是一个完美的正方形。请按照以下步骤解决问题:
- 初始化尺寸为N x N的矩阵mat [] [] 。
- 将矩阵的第一行初始化为{1 2 3…N}。
- 现在的矩阵的剩余行,由1填充由矩阵中的前一行的布置的圆形左移的每一行。
- 完成上述步骤后,打印矩阵。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
void diagonalSumPerfectSquare(int arr[], int N)
{
// Iterate for next N - 1 rows
for(int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for(int j = 0; j < N; j++)
{
cout << (arr[(j + i) % 7]) << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
// Given N
int N = 7;
int arr[N];
// Fill the array with elements
// ranging from 1 to N
for(int i = 0; i < N; i++)
{
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
// This code is contributed by gauravrajput1
Java
// Java program for the above approach
class GFG {
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
static void diagonalSumPerfectSquare(int[] arr,
int N)
{
// Iterate for next N - 1 rows
for (int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for (int j = 0; j < N; j++)
{
System.out.print(arr[(j + i) % 7] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] srgs)
{
// Given N
int N = 7;
int[] arr = new int[N];
// Fill the array with elements
// ranging from 1 to N
for (int i = 0; i < N; i++)
{
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to print the matrix whose sum
# of element in secondary diagonal is a
# perfect square
def diagonalSumPerfectSquare(arr, N):
# Print the current row
print(*arr, sep =" ")
# Iterate for next N - 1 rows
for i in range(N-1):
# Perform left shift by 1
arr = arr[i::] + arr[:i:]
# Print the current row after
# the left shift
print(*arr, sep =" ")
# Driver Code
# Given N
N = 7
arr = []
# Fill the array with elements
# ranging from 1 to N
for i in range(1, N + 1):
arr.append(i)
# Function Call
diagonalSumPerfectSquare(arr, N)
C#
// C# program for the
// above approach
using System;
class GFG {
// Function to print the matrix whose sum
// of element in secondary diagonal is a
// perfect square
static void diagonalSumPerfectSquare(int[] arr,
int N)
{
// Iterate for next N - 1 rows
for (int i = 0; i < N; i++)
{
// Print the current row after
// the left shift
for (int j = 0; j < N; j++)
{
Console.Write(arr[(j + i) % 7] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main(String[] srgs)
{
// Given N
int N = 7;
int[] arr = new int[N];
// Fill the array with elements
// ranging from 1 to N
for (int i = 0; i < N; i++) {
arr[i] = i + 1;
}
// Function Call
diagonalSumPerfectSquare(arr, N);
}
}
// This code is contributed by 29AjayKumar
输出
1 2 3 4 5 6 7
2 3 4 5 6 7 1
3 4 5 6 7 1 2
4 5 6 7 1 2 3
5 6 7 1 2 3 4
6 7 1 2 3 4 5
7 1 2 3 4 5 6
时间复杂度: O(N 2 )
辅助空间: O(N)