# 查找由给定规则生成的矩阵元素的总和

2021年5月1日18:11:42 发表评论 108 次浏览

## 本文概述

1. 第一行将包含一个元素, 即一种其余元素将是0.
2. 下一行将包含两个要素所有的都是(A + B)其余的是0s.
3. 第三行将包含(A + B + B)三遍, 其余的0s.
4. …..
5. 矩阵将仅包含R行。

5 0 0

8 8 0

11 11 11

## C ++

``````//C++ implementation of the approach
#include <iostream>
using namespace std;

//Function to return the required sum
int sum( int A, int B, int R)
{

//To store the sum
int sum = 0;

//For every row
for ( int i = 1; i <= R; i++) {

//Update the sum as A appears i number
//of times in the current row
sum = sum + (i * A);

//Update A for the next row
A = A + B;
}

//Return the sum
return sum;
}

//Driver code
int main()
{

int A = 5, B = 3, R = 3;
cout <<sum(A, B, R);

return 0;
}``````

## Java

``````//JAVA implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG
{

//Function to return the required sum
static int sum( int A, int B, int R)
{

//To store the sum
int sum = 0 ;

//For every row
for ( int i = 1 ; i <= R; i++)
{

//Update the sum as A appears i number
//of times in the current row
sum = sum + (i * A);

//Update A for the next row
A = A + B;
}

//Return the sum
return sum;
}

//Driver code
public static void main (String[] args)
throws java.lang.Exception
{
int A = 5 , B = 3 , R = 3 ;

System.out.print(sum(A, B, R));
}
}

//This code is contributed by nidhiva``````

## Python3

``````# Python3 implementation of the approach

# Function to return the required ssum
def Sum (A, B, R):

# To store the ssum
ssum = 0

# For every row
for i in range ( 1 , R + 1 ):

# Update the ssum as A appears i number
# of times in the current row
ssum = ssum + (i * A)

# Update A for the next row
A = A + B

# Return the ssum
return ssum

# Driver code
A, B, R = 5 , 3 , 3
print ( Sum (A, B, R))

# This code is contributed by Mohit Kumar``````

## C#

``````//C# implementation of the approach
using System;

class GFG
{

//Function to return the required sum
static int sum( int A, int B, int R)
{

//To store the sum
int sum = 0;

//For every row
for ( int i = 1; i <= R; i++)
{

//Update the sum as A appears i number
//of times in the current row
sum = sum + (i * A);

//Update A for the next row
A = A + B;
}

//Return the sum
return sum;
}

//Driver code
public static void Main ()
{
int A = 5, B = 3, R = 3;

Console.Write(sum(A, B, R));
}
}

//This code is contributed by anuj_67..``````

``54`` 