📜  矩形内所有可能的正方形的面积总和

📅  最后修改于: 2021-05-31 17:03:16             🧑  作者: Mango

给定两个整数L和B分别表示矩形的长度和宽度。任务是计算矩形中所有可能的正方形的面积之和。
例子

Input: L = 4, B = 3
Output: 54

Input:  L = 2, B = 5
Output: 26

这个想法是观察一个矩形的平方数。

现在,面1的平方数将为12,因为将有两种情况,一种是沿水平(3)的1单元边的正方形,第二种是沿垂直(4)的1单元边的正方形。这使我们得到3 * 4 = 12个正方形。
当边为2个单位时,一种情况将是仅沿一个位置水平放置2个单位的边的正方形,而第二个情况将为垂直放置两个位置的情况。所以平方数= 6
因此,我们可以推断出这一点,
大小为1 * 1的正方形数将为L * B
尺寸2 * 2的平方数将为(L-1)(B-1)
因此,具有大小的平方数K  将:

因此,大小为K的平方总数的面积为:

下面是上述想法的实现:

C++
// CPP program to calculate the sum of area
// of all possible squares that comes
// inside the rectangle
 
#include 
using namespace std;
 
// Function to calculate the sum of area
// of all possible squares that comes
// inside the rectangle
int calculateAreaSum(int l, int b)
{
    int size = 1;
     
    // Square with max size possible
    int maxSize = min(l,b);
     
    int totalArea = 0;
     
    for(int i=1; i <= maxSize; i++)
    {  
        // calculate total square of a given size
        int totalSquares = (l-size+1)*(b-size+1);
         
        // calculate area of squares of a
        // particular size   
        int area = totalSquares*size*size;
         
        // total area   
        totalArea += area;
          
        // increment size   
        size++;
    }
     
    return totalArea;
}
 
// Driver Code
int main()
{
    int l = 4, b = 3;
     
    cout<


Java
// Java program to calculate the
// sum of area of all possible
// squares that comes inside
// the rectangle
class GFG
{
// Function to calculate the
// sum of area of all possible
// squares that comes inside
// the rectangle
static int calculateAreaSum(int l, int b)
{
    int size = 1;
 
    // Square with max size possible
    int maxSize = Math.min(l, b);
 
    int totalArea = 0;
 
    for(int i = 1; i <= maxSize; i++)
    {
        // calculate total square
        // of a given size
        int totalSquares = (l - size + 1) *
                           (b - size + 1);
     
        // calculate area of squares
        // of a particular size
        int area = totalSquares *  
                    size * size;
     
        // total area
        totalArea += area;
         
        // increment size
        size++;
    }
 
    return totalArea;
}
 
// Driver Code
public static void main(String[] args)
{
    int l = 4, b = 3;
     
    System.out.println(calculateAreaSum(l, b));
}
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to calculate
# the sum of area of all possible
# squares that comes inside
# the rectangle
 
# Function to calculate the
# sum of area of all possible
# squares that comes inside
# the rectangle
def calculateAreaSum(l, b):
    size = 1
     
    # Square with max size possible
    maxSize = min(l, b)
     
    totalArea = 0
     
    for i in range(1, maxSize + 1 ):
 
        # calculate total square
        # of a given size
        totalSquares = ((l - size + 1) *
                        (b - size + 1))
         
        # calculate area of squares
        # of a particular size
        area = (totalSquares *
                size * size)
         
        # total area
        totalArea += area
         
        # increment size
        size += 1
         
    return totalArea
 
# Driver Code
if __name__ == "__main__":
    l = 4
    b = 3
     
    print(calculateAreaSum(l,b))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to calculate the
// sum of area of all possible
// squares that comes inside
// the rectangle
using System;
 
class GFG
{
// Function to calculate the
// sum of area of all possible
// squares that comes inside
// the rectangle
static int calculateAreaSum(int l,
                            int b)
{
    int size = 1;
 
    // Square with max size possible
    int maxSize = Math.Min(l, b);
 
    int totalArea = 0;
 
    for(int i = 1; i <= maxSize; i++)
    {
        // calculate total square
        // of a given size
        int totalSquares = (l - size + 1) *
                           (b - size + 1);
     
        // calculate area of squares
        // of a particular size
        int area = totalSquares *
                   size * size;
     
        // total area
        totalArea += area;
         
        // increment size
        size++;
    }
    return totalArea;
}
 
// Driver Code
public static void Main()
{
    int l = 4, b = 3;
     
    Console.Write(calculateAreaSum(l,b));
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:
54