给定一个整数N 。任务是生成具有以下属性的N*N阶对称矩阵。
- 主对角线应仅包含 0
- 矩阵应仅包含从 0 到 N-1 的元素。
例子:
Input: N = 4
Output:
0 2 3 1
2 0 1 3
3 1 0 2
1 3 2 0
Input: N = 5
Output:
0 2 3 4 1
2 0 4 1 3
3 4 0 2 1
4 1 2 0 3
1 3 1 3 0
处理方法:由于需要的矩阵必须是方阵,我们可以生成一个对称矩阵,其中包含从1到n-1的元素,不包括0。后面会处理0的情况。
以 N = 4 为例:
我们首先生成一个对称矩阵,它可以通过以循环顺序填充从 1 到 n-1 的每一行来轻松完成,即用 1 2 3 填充第一行,并以循环顺序对所有后续行执行此操作。
So, the final matrix will be,
1 2 3
2 3 1
3 1 2
现在,我们生成了一个对称矩阵,其中包含从 1 到 n 的元素。让我们讨论案例 0。我们将利用上述矩阵对称的好处,我们将添加一列 0 和 0 行,如下所示,
1 2 3 0
2 3 1 0
3 1 2 0
0 0 0 0
现在,我们必须将所有 0 放在对角线上。为此,我们将从第一行开始直到最后 1 行,并将所有 0 与每行中的数字交换,并且还将在最后一行中进行如下更改:
For row 1, we swap 0 and 1 and also put last row’s 1st element with the number we swapped i.e. 1.
0 2 3 1
2 3 1 0
3 1 2 0
1 0 0 0
For row 2 we swap 0 and 3, and make the second element of the last row also 3.
0 2 3 1
2 0 1 3
3 1 2 0
1 3 0 0
and so on…
The final matrix generated will be the required matrix.
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to generate the required matrix
void solve(long long n)
{
long long initial_array[n - 1][n - 1], final_array[n][n];
for (long long i = 0; i < n - 1; ++i)
initial_array[0][i] = i + 1;
// Form cyclic array of elements 1 to n-1
for (long long i = 1; i < n - 1; ++i)
for (long long j = 0; j < n - 1; ++j)
initial_array[i][j]
= initial_array[i - 1][(j + 1) % (n - 1)];
// Store initial array into final array
for (long long i = 0; i < n - 1; ++i)
for (long long j = 0; j < n - 1; ++j)
final_array[i][j] = initial_array[i][j];
// Fill the last row and column with 0's
for (long long i = 0; i < n; ++i)
final_array[i][n - 1] = final_array[n - 1][i] = 0;
for (long long i = 0; i < n; ++i) {
long long t0 = final_array[i][i];
long long t1 = final_array[i][n - 1];
// Swap 0 and the number present
// at the current indexed row
swap(final_array[i][i], final_array[i][n - 1]);
// Also make changes in the last row
// with the number we swapped
final_array[n - 1][i] = t0;
}
// Print the final array
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < n; ++j)
cout << final_array[i][j] << " ";
cout << endl;
}
}
// Driver code
int main()
{
long long n = 5;
solve(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to generate the required matrix
static void solve(long n)
{
long initial_array[][]= new long[(int)n - 1][(int)n - 1],
final_array[][]= new long[(int)n][(int)n];
for (long i = 0; i < n - 1; ++i)
initial_array[0][(int)i] = i + 1;
// Form cyclic array of elements 1 to n-1
for (long i = 1; i < n - 1; ++i)
for (long j = 0; j < n - 1; ++j)
initial_array[(int)i][(int)j]
= initial_array[(int)i - 1][(int)((int)j + 1) % ((int)n - 1)];
// Store initial array into final array
for (long i = 0; i < n - 1; ++i)
for (long j = 0; j < n - 1; ++j)
final_array[(int)i][(int)j] = initial_array[(int)i][(int)j];
// Fill the last row and column with 0's
for (long i = 0; i < n; ++i)
final_array[(int)i][(int)n - 1] = final_array[(int)n - 1][(int)i] = 0;
for (long i = 0; i < n; ++i)
{
long t0 = final_array[(int)i][(int)i];
long t1 = final_array[(int)i][(int)n - 1];
// Swap 0 and the number present
// at the current indexed row
long s = final_array[(int)i][(int)i];
final_array[(int)i][(int)i]=final_array[(int)i][(int)n - 1];
final_array[(int)i][(int)n - 1]=s;
// Also make changes in the last row
// with the number we swapped
final_array[(int)n - 1][(int)i] = t0;
}
// Print the final array
for (long i = 0; i < n; ++i)
{
for (long j = 0; j < n; ++j)
System.out.print( final_array[(int)i][(int)j] + " ");
System.out.println();
}
}
// Driver code
public static void main(String args[])
{
long n = 5;
solve(n);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python 3 implementation of the approach
# Function to generate the required matrix
def solve(n):
initial_array = [[0 for i in range(n-1)] for j in range(n-1)]
final_array = [[0 for i in range(n)]for j in range(n)]
for i in range(n - 1):
initial_array[0][i] = i + 1
# Form cyclic array of elements 1 to n-1
for i in range(1, n - 1):
for j in range(n - 1):
initial_array[i][j] = initial_array[i - 1][(j + 1) % (n - 1)]
# Store initial array into final array
for i in range(n-1):
for j in range(n-1):
final_array[i][j] = initial_array[i][j]
# Fill the last row and column with 0's
for i in range(n):
final_array[i][n - 1] = final_array[n - 1][i] = 0
for i in range(n):
t0 = final_array[i][i]
t1 = final_array[i][n - 1]
# Swap 0 and the number present
# at the current indexed row
temp = final_array[i][i]
final_array[i][i] = final_array[i][n - 1]
final_array[i][n - 1] = temp
# Also make changes in the last row
# with the number we swapped
final_array[n - 1][i] = t0
# Print the final array
for i in range(n):
for j in range(n):
print(final_array[i][j],end = " ")
print("\n",end = "")
# Driver code
if __name__ == '__main__':
n = 5
solve(n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to generate the required matrix
static void solve(long n)
{
long [,]initial_array = new long[(int)n - 1,(int)n - 1];
long [,]final_array = new long[(int)n,(int)n];
for (long i = 0; i < n - 1; ++i)
initial_array[0,(int)i] = i + 1;
// Form cyclic array of elements 1 to n-1
for (long i = 1; i < n - 1; ++i)
for (long j = 0; j < n - 1; ++j)
initial_array[(int)i,(int)j]
= initial_array[(int)i - 1,(int)((int)j + 1) % ((int)n - 1)];
// Store initial array into final array
for (long i = 0; i < n - 1; ++i)
for (long j = 0; j < n - 1; ++j)
final_array[(int)i,(int)j] = initial_array[(int)i,(int)j];
// Fill the last row and column with 0's
for (long i = 0; i < n; ++i)
final_array[(int)i,(int)n - 1] = final_array[(int)n - 1,(int)i] = 0;
for (long i = 0; i < n; ++i)
{
long t0 = final_array[(int)i, (int)i];
long t1 = final_array[(int)i, (int)n - 1];
// Swap 0 and the number present
// at the current indexed row
long s = final_array[(int)i,(int)i];
final_array[(int)i,(int)i] = final_array[(int)i, (int)n - 1];
final_array[(int)i,(int)n - 1] = s;
// Also make changes in the last row
// with the number we swapped
final_array[(int)n - 1,(int)i] = t0;
}
// Print the final array
for (long i = 0; i < n; ++i)
{
for (long j = 0; j < n; ++j)
Console.Write( final_array[(int)i,(int)j] + " ");
Console.WriteLine();
}
}
// Driver code
public static void Main(String []args)
{
long n = 5;
solve(n);
}
}
// This code contributed by Rajput-Ji
PHP
Javascript
0 2 3 4 1
2 0 4 1 3
3 4 0 2 1
4 1 2 0 3
1 3 1 3 0
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。