📜  第N次折叠后正方形面积的程序

📅  最后修改于: 2021-10-23 08:51:38             🧑  作者: Mango

正方形是一个平面上的平面形状,由四个角的四个点定义。一个正方形有四个等长的边和四个角,都是直角(90 度角)。正方形是长方形的一种。
给定一个正方形的边N  和折叠次数F  .任务是找到第F 次折叠后的正方形面积。
正方形的折叠如下:

  1. 在第一次折叠中,将正方形从左到右折叠成三角形。
  2. 在第二次折叠中,从上到下折叠正方形。
  3. 在第三次折叠中,再次从左到右折叠正方形。

例子:

Input : N = 4, F = 2
Output : 2
Explanation: 
Initially square side is 4 x 4
After 1st folding, square side becomes 4 x 2
After 2nd folding, square side becomes 2 x 2
Thus area equals 2 x 2 = 4.

Input : N = 100, F = 6
Output : 156.25

方法

  • 折叠前首先计算正方形的面积。
  • 每次折叠后,正方形的面积减少一半。也就是说, area = area/2
  • 所以,我们最终将正方形的面积除以 pow(2, F)

下面是上述方法的实现:

C++
// CPP program to find
// the area of the square
#include 
using namespace std;
 
// Function to calculate area of square after
// given number of folds
double areaSquare(double side, double fold)
{
    double area = side * side;
 
    return area * 1.0 / pow(2, fold);
}
 
// Driver Code
int main()
{
    double side = 4, fold = 2;
 
    cout << areaSquare(side, fold);
 
    return 0;
}


Java
// Java program to find the area of the square
class GFG
{
 
    // Function to calculate area of square
    // after given number of folds
    static double areaSquare(double side,
                            double fold)
    {
        double area = side * side;
     
        return area * 1.0 / Math.pow(2, fold);
    }
     
    // Driver Code
    public static void main(String []args)
    {
        double side = 4, fold = 2;
     
        System.out.println(areaSquare(side, fold));
    }
}
 
// This code is contributed
// by aishwarya.27


Python3
# Python3 program to find the area
# of the square
 
# Function to calculate area of
# square after given number of folds
def areaSquare(side, fold) :
        area = side * side
        ans = area / pow(2, fold)
        return ans
 
# Driver Code
if __name__ == "__main__" :
         
    side = 4
    fold = 2
    print(areaSquare(side, fold))
 
# This code is contributed by Ryuga


C#
// C# program to find the area of the square
using System;
 
class GFG
{
     
// Function to calculate area of square
// after given number of folds
static double areaSquare(double side,
                         double fold)
{
    double area = side * side;
 
    return area * 1.0 / Math.Pow(2, fold);
}
 
// Driver Code
public static void Main()
{
    double side = 4, fold = 2;
 
    Console.Write(areaSquare(side, fold));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
4