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

📅  最后修改于: 2021-04-23 20:25:03             🧑  作者: Mango

正方形是在一个平面上的平面形状,由四个角的四个点定义。一个正方形具有四个等长的边和四个直角(90度角)的角。正方形是一种矩形。

给定一个边的平方N和倍数F 。任务是在F折后找到正方形的面积。

正方形的折叠如下:

  1. 在第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


输出:
4