简体   繁体   English

如何在矩阵中得到这个三角形?

[英]How to get this triangle in matrix?

在此处输入图像描述

#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
const int n = 5;
void fill_matrix(int (*matrix)[n])
{
    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col < n; col++)
            matrix[row][col] = rand() % 201 - 100;
    }

}

void print_matrix(int (*matrix)[n])
{
    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col < n; col++)
            cout << setw(4) << matrix[row][col];
        cout << std::endl;
    }
}

int main()
{
    int matrix[n][n];
    int sum = 0;
    fill_matrix(matrix);
    print_matrix(matrix);

    for (int row = 0; row < n; row++)
    {
        for (int col = 0; col < n; col++)
        {
        }
    }
    return 0;
}

I can't realize how I can loop over this part of the matrix.我不知道如何遍历矩阵的这一部分。 The matrix can be a different size.矩阵可以是不同的大小。 So I can't imagine an algorithm to get the right triangle of the matrix所以我无法想象一种算法来获得矩阵的直角三角形

It is important to realise, that the widest point of the triangle is at its tip.重要的是要认识到,三角形的最宽点在它的尖端。

Let i = 1 So going from the top row to the bottom, do:i = 1所以从顶行到底行,做:

Start:开始:

  • set the last i fields in this row to 1.将此行中的最后 i 个字段设置为 1。

  • if i is smaller than the height of the matrix, increase i by one如果i小于矩阵的高度,则将i增加 1

  • else if it is greater than the height, decrease it by one否则,如果它大于高度,则将其减一

Go back to start Go 回到开始

Consider the generic nested loops used in the posted code to traverse the "matrix", just written in a slightly different way.考虑发布的代码中用于遍历“矩阵”的通用嵌套循环,只是以稍微不同的方式编写。

for (int row = 0; row < n; row++)
{
    int const first_col = 0;
    int const last_col = n;
    for (int col = first_col; col < last_col; col++)
    {
        // ...
    }
}

At the right side of the greyed area in OP's posted image, every row terminates at the boundary of the matrix (so that we don't really need a last_col variable, the original condition is fine).在 OP 发布的图像中灰色区域的右侧,每一行都终止于矩阵的边界(因此我们真的不需要last_col变量,原始条件很好)。

Note, though, that all the matrix elements inside this area are at the right (or on) of both the leading diagonal and the antidiagonal.但是请注意,该区域内的所有矩阵元素位于主对角线和反对角线的右侧(或上方)。

Now, what would be the mathematical equation of the leading diagonal, given the row?现在,给定行,前导对角线的数学方程式是什么?

column = f(row) = row列 = f(行) = 行

What about the antidiagonal?反对角线呢? Look at its definition (from Wolfram ):查看它的定义(来自Wolfram ):

An antidiagonal is defined as a list of elements for which the sum of the indices are equal, where the first element is in the top-right of a matrix反对角线被定义为索引之和相等的元素列表,其中第一个元素位于矩阵的右上角

So, given a matrix of size nxn and remembering that in C++ array indices are 0-based, we can say:因此,给定一个大小为 nxn 的矩阵并记住 C++ 数组索引是从 0 开始的,我们可以说:

column = g(row) = n - 1 - row列 = g(行) = n - 1 - 行

As already noted, the value we need to initialize first_col in the outer loop is the max of these two values.如前所述,我们需要在外循环中初始化first_col的值是这两个值中的最大值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM