📌  相关文章
📜  通过在中心连接 n 边多边形的顶点形成的循环数

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

给定一个N 边的正多边形,我们已经连接了多边形中心的所有顶点,从而将多边形分成 N 个相等的部分。我们的任务是计算多边形中循环的总数。
注意:循环是在同一点开始和结束的闭环。
例子:

方法:对于上面提到的问题,我们应该计算分割后给定多边形中可能出现的闭环总数。该方法基于数学模式。由于多边形的划分,已经创建了N 个循环N 个块中的一个将与剩余(N – 1)个块形成一个循环。剩余的(N-1)个块将与其他(N-2)个块形成循环。因此,我们可以使用下面给出的公式找出总周期数:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to calculate number of cycles
int findCycles(int N)
{
    int res = 0;
    int finalResult = 0;
    int val = 2 * N - 1;
  
    // BigInteger is used here
    // if N=10^9 then multiply
    // will result into value
    // greater than 10^18
    int s = val;
  
    // BigInteger multiply function
    res = (N - 1) * (N - 2);
    finalResult = res + s;
  
    // Return the final result
    return finalResult;
}
 
// Driver code
int main()
{
    // Given N
    int N = 5;
  
    // Function Call
    cout << findCycles(N) << endl;   
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
 
import java.util.*;
import java.math.*;
 
class GFG {
 
    // Function to calculate number of cycles
    static BigInteger findCycles(int N)
    {
        BigInteger res, finalResult;
        long val = 2 * N - 1;
 
        String st = String.valueOf(val);
 
        // BigInteger is used here
        // if N=10^9 then multiply
        // will result into value
        // greater than 10^18
 
        BigInteger str = new BigInteger(st);
        String n1 = String.valueOf((N - 1));
        String n2 = String.valueOf((N - 2));
 
        BigInteger a = new BigInteger(n1);
        BigInteger b = new BigInteger(n2);
 
        // BigInteger multiply function
        res = a.multiply(b);
 
        finalResult = res.add(str);
 
        // Return the final result
        return finalResult;
    }
 
    // Driver Code
    public static void
    main(String args[]) throws Exception
    {
        // Given N
        int N = 5;
 
        // Function Call
        System.out.println(findCycles(N));
    }
}


Python3
# Python3 program for the above approach
  
# Function to calculate number of cycles
def findCycles(N):
    res = 0
    finalResult = 0
    val = 2 * N - 1;
 
    # BigInteger is used here
    # if N=10^9 then multiply
    # will result into value
    # greater than 10^18
    s = val
 
    # BigInteger multiply function
    res = (N - 1) * (N - 2)
    finalResult = res + s;
 
    # Return the final result
    return finalResult;
 
# Driver Code
if __name__=='__main__':
     
    # Given N
    N = 5;
 
    # Function Call
    print(findCycles(N));
 
    # This code is contributed by pratham76


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to calculate number of cycles
  static int findCycles(int N)
  {
    int res = 0;
    int finalResult = 0;
    int val = 2 * N - 1;
 
    // BigInteger is used here
    // if N=10^9 then multiply
    // will result into value
    // greater than 10^18
    int s = val;
 
    // BigInteger multiply function
    res = (N - 1) * (N - 2);
    finalResult = res + s;
 
    // Return the final result
    return finalResult;
  }
 
  // Driver code
  static void Main()
  {
     
    // Given N
    int N = 5;
 
    // Function Call
    Console.WriteLine(findCycles(N));
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:
21

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程