给定一个整数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
Javascript
输出
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live