计算矩阵中对角线的总和

问题描述 投票:10回答:5

我需要用C ++计算矩阵中两个对角线的总和,我已经有了一个解决方案,但我必须愚蠢,因为我无法理解它在做什么,所以我想知道是否有另一个版本,我可以了解。这是完成工作的代码:

cout<<"Jepi rangun e  matrices"<<endl;  // pra bejme manipulim me matrice katrore ku rreshtat=kolonat
cin>>n;
cout<<"Tani jepi elementet e matrices"<<endl; // lexohet matrica

for(i=1;i<=n;i++)
{
     for(j=1;j<=n;j++)
        cin>>a[i][j];
}

d=0;
s=0; // ketu e keni kushtin si dhe mbledhjen per te dy diagonalet me dy variabla te ndryshme

for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(i==j)
            d=d+a[i][j];
        if(j==n-i+1 || i==n-j+1) 
            s=s+a[i][j];
    }

难以理解的部分是

if(j==n-i+1 || i==n-j+1) 
    s=s+a[i][j];

这是我更改的整个代码,但它不适用于辅助对角线:

#include <iostream>
using namespace std;

int main()
{
    int d=0,s=0; // ketu e keni kushtin si dhe mbledhjen per te dy diagonalet me dy variabla te ndryshme
    int i,j,n;
    int a[5][5];

    cout<<"Jepi rangun e  matrices"<<endl;  // pra bejme manipulim me matrice katrore ku rreshtat=kolonat
    cin>>n;
    cout<<"Tani jepi elementet e matrices"<<endl; // lexohet matrica

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cin>>a[i][j];
    }

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i==j) 
                d+=a[i][j]; //principal diagonal 
            if(i+j==n-1)
                s+=a[i][j];//secondary diagonal

        }
    }

    cout << d << endl;
    cout << s << endl;
    cin.get();
    cin.get();
    return 0;
}
c++ algorithm matrix
5个回答
22
投票

用英语发表评论会很好,但是,你的代码确实如此(第二循环):

browse all rows
  browse all cells
    if i == j (is in main diagonal):
        increase one sum
    if i == n - i + 1 (the other diagonal)
        increase the second sum

更好,更有效的代码(使用n,而不是n^2)将是:

for( int i = 0; i < n; i++){
   d += a[i][i];  // main diagonal
   s += a[i][n-i-1]; // second diagonal (you'll maybe need to update index)
}

这直接通过对角线(两个都在一个循环!),而不是通过其他项目。

编辑:

主对角线有坐标qazxsw poi(因此qazxsw poi)。

次要对角线有坐标(矩阵3x3):{(1,1), (2,2), ..., (i,i)},一般是:i == j。但是在C中,数组的索引是从0开始,而不是1,所以你不需要那个{(1,3), (2,2),(3,1)}(可能)。

次要对角线上的所有项目都必须符合条件:{(1,n-1+1), (2, n-2+1), ... (i, n-i+1), .... (n,1)}(再次由于C的索引从0 +1变为i == n - j + 1+1-1i=0,n=3))。

您可以在一个循环中实现所有这些(上面的代码)。


6
投票
j=2

}

要更好地理解这个算法,你应该在笔记本上画一个矩阵,并用矩阵中的位置对它的元素进行编号,然后逐步应用算法。我百分百肯定你会理解


3
投票

我试着解释这个版本怎么样? :d

代码有3个重要部分:

  • 输入矩阵
  • 计算主要对角线(\方向)
  • 计算小对角线(/方向)

他们在这里解释说:

j = n - i - 1

这里,d和s分别包含主要和次要对角线的值。在2个循环结束时,它们将包含结果

int diag1=0;
int diag2=0;

for (i=0;i<n;i++)
 for (j=0;j<n;j++){

  if(i==j)  diag1+=a[i][j]; //principal diagonal 
  if(i+j==n-1) diag2+=a[i][j];//secondary diagonal

希望这可以帮助。

请注意,此代码以1而不是0开始矩阵坐标,因此您实际上需要为矩阵分配// input elements for(i=1;i<=n;i++) // from left to right { for(j=1;j<=n;j++) // from up to down cin>>a[i][j]; // input element at (i,j) position } 空间:

for (i=1;i<=n;i++)
     for (j=1;j<=n;j++)
     {
        if(i==j)          // major diagonal - if coordinates are the same
           d=d+a[i][j];   // e.g. (1,1), (2,2)
        if(j==n-i+1 || i==n-j+1)  // coordinates of the minor diagonal - check
           s=s+a[i][j];           // e.g. n=3 (3,1) (2,2) ...
      }

在使用它之前。

此外,您提供的代码并不是最有效的。它具有(n+1)x(n+1)的复杂性,而任务可以在double a[n+1][n+1]; 中完成,如下所示:

O(n^2)

0
投票
O(n)

0
投票

您必须使用// matrix coordinates now start from 0 for (int i=0; i < n; ++i){ d += a[i][i]; // major s += a[i][n-1-i]; // minor } 而不是int num[5][5]={0}; //decleration int i=0,j=0,sum=0; for (int i=0;i<5;i++) { for (int j=0;j<5;j++) { cin>>num[i][j]; } //Taking Matrix input } cout<<endl<<"The Matrix is "<<endl; for (int i=0;i<5;i++) { for (int j=0;j<5;j++) { cout<<num[i][j]<<" "; } cout<<endl; //Displaying the Matrix } cout<<endl<<"The sum of diagonals of the matrix is "<<endl; if(i==j) { for (i=0;i<5;i++) { for (j=0;j<5;j++) { if (i==j) //This loop works where i and j will be equal { sum=sum+num[i][j]; } } } cout<<sum; } else //Some times the user creates 4 x 3 matrix or so than diagonals not match so. . . { cout<<"The sum is not Possible"; } 作为次要对角线,即

i + j == n + 1
© www.soinside.com 2019 - 2024. All rights reserved.