📜  查找一个N阶对称矩阵,该矩阵包含从0到N-1的整数,并且主对角线应仅包含0

📅  最后修改于: 2021-05-08 17:34:29             🧑  作者: Mango

给定整数N。任务是生成具有以下属性的N * N阶对称矩阵。

  1. 主对角线应仅包含0
  2. 矩阵应仅包含从0到N-1的元素。

例子:

方法:由于所需矩阵必须是方矩阵,因此我们可以生成一个包含从1到n-1(不包括0)的元素的对称矩阵。稍后将处理0的情况。
以N = 4时为例:
我们首先生成一个对称矩阵,可以很容易地通过以循环顺序填充从1到n-1的每一行来完成,即以1 2 3填充第一行,然后以循环顺序对所有后续行执行此操作。

现在,我们生成了一个包含从1到n的元素的对称矩阵。让我们讨论案例0。我们将利用上述矩阵对称的优势,像这样添加0的列和0的行,

现在,我们必须将所有0放在对角线上。为此,我们将从第一行开始直到last-1行,并将所有0替换为每一行中存在的数字,并在最后一行中进行如下更改:

下面是上述方法的实现:

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


输出:
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