原文:

给定一个正整数 n ,任务是打印包含任意一个一位数随机正整数作为非零元素的高阶赫森伯格矩阵 n 。 是一个正方形矩阵,其中以下的所有元素都为零。在数学术语中,mat[i][j] = 0 表示所有 i > j 1举例:

输入: n = 3 输出: 1 2 8 1 3 4 0 3 4 输入: n = 4 输出: 1 2 2 3 1 3 4 2 0 3 4 2 0 1 4

方法:为了打印具有一位数正元素的上部赫森伯格矩阵,在函数的帮助下,为矩阵中的所有单元打印零,其中 i > j 1 和任何一位数随机数。 以下是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// function to print the upper hessenberg
// matrix of order n
void upperhessenbergmatrix(int n)
{
    for (int i = 1; i <= n; i  ) {
        for (int j = 1; j <= n; j  ) {
            // if element is below sub-diagonal
            // then print 0
            if (i > j   1)
                cout << '0' << " ";
            // print a random digit for
            // every non-zero element
            else
                cout << rand() % 10 << " ";
        }
        cout << "\n";
    }
}
// driver code
int main()
{
    int n = 4;
    upperhessenbergmatrix(n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
class gfg
{
// function to print the lower hessenberg
// matrix of order n
static void upperhessenbergmatrix(int n)
{
    for (int i = 1; i <= n; i  )
    {
        for (int j = 1; j <= n; j  )
        {
            // if element is above super-diagonal
            // then print 0
            if (i > j   1)
            {
                system.out.print(0   " ");
            }
            // print a random digit for
            // every non-zero element
            else
            {
                system.out.print((int)(math.random() * 10)   " ");
            }
        }
        system.out.println();
    }
}
// driver code
public static void main(string[] args)
{
    int n = 4;
    upperhessenbergmatrix(n);
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 implementation of the approach
import random
# function to print the upper hessenberg
# matrix of order n
def upperhessenbergmatrix(n):
    for i in range(1, n   1):
        for j in range(1, n   1):
            # if element is below sub-diagonal
            # then pr0
            if (i > j   1):
                print('0', end = " ")
            # pra random digit for
            # every non-zero element
            else:
                print(random.randint(1, 10),
                                 end = " ")
        print()
# driver code
n = 4;
upperhessenbergmatrix(n)
# this code is contributed
# by mohit kumar

c

// c# implementation of the approach
using system;
class gfg
{
    // function to print the lower hessenberg
    // matrix of order n
    static void upperhessenbergmatrix(int n)
    {
        random rand = new random();
        for (int i = 1; i <= n; i  )
        {
            for (int j = 1; j <= n; j  )
            {
                // if element is above super-diagonal
                // then print 0
                if (i > j   1)
                    console.write(0   " ");
                // print a random digit for
                // every non-zero element
                else
                    console.write(rand.next(1, 10)   " ");
            }
            console.writeline();
        }
    }
    // driver code
    static public void main ()
    {
        int n = 4;
        upperhessenbergmatrix(n);
    }
}   
// this code is contributed by ankitrai01

java 描述语言


output: 

3 6 7 5 
3 5 6 2 
0 9 1 2 
0 0 7 0