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

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

null
Input: L = 4, B = 3Output: 54Input:  L = 2, B = 5Output: 26

目的是观察 矩形中的正方形数 .

图片[1]-矩形内所有可能正方形的面积之和-yiteyi-C++库

现在,边1的正方形数量将是12,因为有两种情况,一种是水平方向的1单位边的正方形(3),另一种是垂直方向的1单位边的正方形(4)。这给了我们3*4=12平方。 当边长为2个单位时,一种情况下,两个单位的边长沿水平方向只有一个地方呈正方形,另一种情况下,两个地方呈垂直方向。所以平方数=6 所以我们可以推断, 大小为1*1的方块数为L*B 尺寸为2*2的正方形数量为(L-1)(B-1) 因此,正方形的数量与大小有关 K  将:

大小为K=(L-K+1)*(B-K+1)的正方形的数量

因此,大小为K的正方形的总面积为:

大小为K=(L-K+1)*(B-K+1)*K*K的正方形总面积

以下是上述理念的实施:

C++

// CPP program to calculate the sum of area
// of all possible squares that comes
// inside the rectangle
#include <bits/stdc++.h>
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<<calculateAreaSum(l,b);
return 0;
}


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

<?php
// PHP 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
function calculateAreaSum( $l , $b )
{
$size = 1;
// Square with max size possible
$maxSize = min( $l , $b );
$totalArea = 0;
for ( $i = 1; $i <= $maxSize ; $i ++)
{
// 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 ++;
}
return $totalArea ;
}
// Driver Code
$l = 4;
$b = 3;
echo calculateAreaSum( $l , $b );
// This code is contributed
// by ChitraNayal
?>


Javascript

<script>
// Javascript 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
function calculateAreaSum(l, b)
{
var size = 1;
// Square with max size possible
var maxSize = Math.min(l,b);
var totalArea = 0;
for ( var i=1; i <= maxSize; i++)
{
// calculate total square of a given size
var totalSquares = (l-size+1)*(b-size+1);
// calculate area of squares of a
// particular size
var area = totalSquares*size*size;
// total area
totalArea += area;
// increment size
size++;
}
return totalArea;
}
// Driver Code
var l = 4, b = 3;
document.write( calculateAreaSum(l,b));
// This code is contributed by noob2000.
</script>


输出:

54

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享