给定一个数字N ,它表示多边形的边数,其中多边形的质心与所有顶点相连,任务是找到多边形中的循环数。
例子:
Input: N = 4
Output: 13
Input: N = 8
Output: 57
方法:这个问题遵循一个简单的方法,我们可以使用一个简单的公式来找到多边形中从质心到顶点的线的循环数。为了推导出循环数,我们可以使用这个公式:
(N) * (N – 1) + 1
如果 N 的值为 4,我们可以使用这个简单的公式来找到 Cycles 的数量,即 13。类似地,如果 N 的值为 10,那么 Cycles 的数量将为 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。