在给定基底的直角等腰三角形中,2×2个单位大小的正方形的最大数量是多少(单位)。 正方形的一侧必须与三角形的底部平行。
null
例如:
Input : 8 Output : 6 Please refer below diagram for explanation.
Input : 7 Output : 3
因为三角形是等腰的,所以给定的底也等于高度。现在在对角线部分,我们总是需要一个额外的长度2个单位的高度和三角形的基础,以适应一个三角形。(图中三角形的CF和AM段。不构成任何正方形的部分)。在基底的剩余长度中,我们可以构造长度为1/2的正方形。因为每个正方形有两个单位,所以高度的情况也是一样,不需要再计算。 因此,对于给定长度的每一层,我们可以构造“(长度-2)/2”平方。这为我们提供了一个“长度-2”的基础。继续这个过程,以获得所有可用“长度2”高度的方块数,我们可以计算方块数。
while length > 2 answer += (length - 2 )/2 length = length - 2
为了更有效的方法,我们可以使用AP n*(n+1)/2之和的公式,其中n=长度–2
C++
// C++ program to count number of 2 x 2 // squares in a right isosceles triangle #include<bits/stdc++.h> using namespace std; int numberOfSquares( int base) { // removing the extra part we would // always need base = (base - 2); // Since each square has base of // length of 2 base = floor (base / 2); return base * (base + 1)/2; } // Driver code int main() { int base = 8; cout << numberOfSquares(base); return 0; } // This code is improved by heroichitesh. |
JAVA
// Java program to count number of 2 x 2 // squares in a right isosceles triangle class Squares { public static int numberOfSquares( int base) { // removing the extra part // we would always need base = (base - 2 ); // Since each square has // base of length of 2 base = Math.floorDiv(base, 2 ); return base * (base + 1 )/ 2 ; } // Driver code public static void main(String args[]) { int base = 8 ; System.out.println(numberOfSquares(base)); } } // This code is contributed by Anshika Goyal and improved by heroichitesh. |
Python3
# Python3 program to count number # of 2 x 2 squares in a right # isosceles triangle def numberOfSquares(base): # removing the extra part we would # always need base = (base - 2 ) # Since each square has base of # length of 2 base = base / / 2 return base * (base + 1 ) / 2 # Driver code base = 8 print (numberOfSquares(base)) # This code is contributed by Anant Agarwal and improved by heroichitesh. |
C#
// C# program to count number of 2 x 2 // squares in a right isosceles triangle using System; class GFG { public static int numberOfSquares( int _base) { // removing the extra part // we would always need _base = (_base - 2); // Since each square has // base of length of 2 _base = _base / 2; return _base * (_base + 1)/2; } // Driver code public static void Main() { int _base = 8; Console.WriteLine(numberOfSquares(_base)); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to count number of 2 x 2 // squares in a right isosceles triangle function numberOfSquares( $base ) { // removing the extra // part we would // always need $base = ( $base - 2); // Since each square // has base of // length of 2 $base = intdiv( $base , 2); return $base * ( $base + 1)/2; } // Driver code $base = 8; echo numberOfSquares( $base ); // This code is contributed by anuj_67 and improved by heroichitesh. ?> |
Javascript
<script> // Program to count number of 2 x 2 // squares in a right isosceles triangle function numberOfSquares(base) { // Removing the extra part we would // always need base = (base - 2); // Since each square has base of // length of 2 base = Math.floor(base / 2); return base * (base + 1) / 2; } // Driver code let base = 8; document.write(numberOfSquares(base)); // This code is contributed by Mayank Tyagi </script> |
输出:
6
本文由 哈希特·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END