给定数字N ,该数字N表示多边形的质心与所有顶点连接的多边形的边数,任务是找到多边形中的循环数。
例子:
Input: N = 4
Output: 13
Input: N = 8
Output: 57
方法:这个问题遵循一种简单的方法,在该方法中,我们可以使用一个简单的公式来计算从质心到顶点的线在多边形中的循环数。要推断出循环数,我们可以使用以下公式:
(N) * (N – 1) + 1
如果N的值为4,我们可以使用这个简单的公式来查找循环数为13。以类似的方式,如果N的值为10,则循环数将为91。
下面是上述方法的实现:
C++
// C++ program to find number
// of cycles in a Polygon with
// lines from Centroid to Vertices
#include
using namespace std;
// Function to find the Number of Cycles
int nCycle(int N)
{
return (N) * (N - 1) + 1;
}
// Driver code
int main()
{
int N = 4;
cout << nCycle(N)
<< endl;
return 0;
}
Java
// Java program to find number
// of cycles in a Polygon with
// lines from Centroid to Vertices
class GFG{
// Function to find the Number of Cycles
static int nCycle(int N)
{
return (N) * (N - 1) + 1;
}
// Driver code
public static void main (String[] args)
{
int N = 4;
System.out.println(nCycle(N));
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to find number
# of cycles in a Polygon with
# lines from Centroid to Vertices
# Function to find the Number of Cycles
def nCycle(N):
return (N) * (N - 1) + 1
# Driver code
N = 4
print(nCycle(N))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find number
// of cycles in a Polygon with
// lines from Centroid to Vertices
using System;
class GFG{
// Function to find the Number of Cycles
static int nCycle(int N)
{
return (N) * (N - 1) + 1;
}
// Driver code
public static void Main (String[] args)
{
int N = 4;
Console.Write(nCycle(N));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
13
时间复杂度: O(1)
辅助空间: O(1)