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

📅  最后修改于: 2021-06-25 15:13:08             🧑  作者: 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