📜  上三角和下三角之和

📅  最后修改于: 2022-05-13 01:57:22.783000             🧑  作者: Mango

上三角和下三角之和

给定一个矩阵,打印上下三角形元素的总和(即对角线上的元素以及上下元素)。
例子 :

Input : {6, 5, 4}
        {1, 2, 5}
        {7, 9, 7}
Output :
Upper sum is 29
Lower sum is 32

解决方案很简单,我们只需要遍历矩阵并相应地计算上下三角形的和即可。

C++
// C++ program to calculate the sum
// of upper and lower triangle
#include 
using namespace std;
 
/*function to calculate sum*/
void sum(int mat[3][3], int r, int c)
{
    int i, j;
    int upper_sum = 0;
    int lower_sum = 0;
     
    /*to calculate sum of upper triangle*/
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (i <= j) {
                upper_sum += mat[i][j];
            }
        }
 
    printf("Upper sum is %d\n", upper_sum);
     
    /*to calculate sum of lower*/
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (j <= i) {
                lower_sum += mat[i][j];
            }
        }
 
    printf("Lower sum is %d", lower_sum);
}
 
/*driver function*/
int main()
{
    int r = 3;
    int c = 3;
 
    /*giving the matrix*/
    int mat[3][3] = {{ 6, 5, 4 },
                     { 1, 2, 5 },
                     { 7, 9, 7 }};
                      
    /*calling the function*/
    sum(mat, r, c);
    return 0;
}


Java
// Java program to calculate the sum
// of upper and lower triangle
 
class GFG
{
    /*function to calculate sum*/
    static void sum(int mat[][], int r, int c)
    {
        int i, j;
        int upper_sum = 0;
        int lower_sum = 0;
         
        /*calculate sum of upper triangle*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++)
            {
                if (i <= j)
                {
                    upper_sum += mat[i][j];
                }
            }
     
        System.out.println("Upper sum is " + upper_sum);
         
        /*calculate sum of lower*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++)
            {
                if (j <= i)
                {
                    lower_sum += mat[i][j];
                }
            }
     
        System.out.print("Lower sum is " + lower_sum);
    }
         
    // Driver code
    public static void main (String[] args)
    {
        int r = 3;
        int c = 3;
     
        /*giving the matrix*/
        int mat[][] = {{ 6, 5, 4 },
                        { 1, 2, 5 },
                        { 7, 9, 7 }};
                         
        /*calling the function*/
        sum(mat, r, c);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to calculate the sum
# of upper and lower triangle
 
# function to calculate sum
def Sum(mat, r, c):
 
    i, j = 0, 0;
    upper_sum = 0;
    lower_sum = 0;
     
    # to calculate sum of upper triangle
    for i in range(r):
        for j in range(c):
            if (i <= j):
                upper_sum += mat[i][j];
 
    print("Upper sum is ", upper_sum);
     
    # to calculate sum of lower
    for i in range(r):
        for j in range(c):
            if (j <= i):
                lower_sum += mat[i][j];
 
    print("Lower sum is ", lower_sum);
 
# Driver Code
r = 3;
c = 3;
 
# giving the matrix
mat = [[ 6, 5, 4 ],
       [ 1, 2, 5 ],
       [ 7, 9, 7 ]];
 
# calling the function
Sum(mat, r, c);
 
# This code is contributed by 29AjayKumar


C#
// C# program to calculate the sum
// of upper and lower triangle
using System;
 
class GFG
{
    /*function to calculate sum*/
    static void sum(int [,]mat, int r, int c)
    {
        int i, j;
        int upper_sum = 0;
        int lower_sum = 0;
         
        /*calculate sum of upper triangle*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++)
            {
                if (i <= j)
                {
                   upper_sum += mat[i,j];
                }
            }
     
        Console.WriteLine("Upper sum is " +
                                upper_sum);
         
        /*calculate sum of lower*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++)
            {
                if (j <= i)
                {
                   lower_sum += mat[i,j];
                }
            }
     
        Console.Write("Lower sum is " +
                            lower_sum);
    }
         
    // Driver code
    public static void Main ()
    {
        int r = 3;
        int c = 3;
     
        /*giving the matrix*/
        int [,]mat = {{6, 5, 4},
                      {1, 2, 5},
                      {7, 9, 7}};
                         
        /*calling the function*/
        sum(mat, r, c);
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出 :

Upper sum is 29
Lower sum is 32