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

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

本文概述

给定三个整数A、B和R,任务是找到由给定规则生成的矩阵的所有元素的和:

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

例如,如果A = 5, B = 3, R = 3,则矩阵为:

5 0 0

8 8 0

11 11 11

例子:

输入:A = 5, B = 3, R = 3
输出:54 5 + 8 + 8 + 11 + 11 + 11 = 54
输入:A = 7, B = 56, R = 1
输出:7

方法:初始化总和= 0而对于每个1≤i≤R更新和=和+(i * A)。每次迭代更新后A = A + B。最后打印最终金额。

下面是上述方法的实现:

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

一盏木

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: