给定两个正整数 N 和 M,它们表示凸正多边形的边,其中 N < M,任务是检查多边形是否具有相同的中心,如果 N 边多边形内接在 M 边多边形中。
多边形中心:指向与多边形的每个顶点等距的多边形内部。
例子:
Input: N = 9, M = 3
Output: YES
Explanation:
Polygon of side 3 when inscribed in a polygon of side 9, then both polygons have same center.
Input: N = 10, M = 3
Output: NO
Explanation:
Polygon of side 3 when inscribed in a polygon of side 10, then both polygons don’t have same center.
方法:这个问题的关键观察是当M % N == 0 时,这意味着 N 边多边形的边相等地覆盖 M 边多边形的边,这意味着两个多边形具有相同的中心。
算法:
- 检查 M 是否可被 N 整除,如果是,则两个多边形具有相同的中心。
- 否则两个多边形具有不同的中心。
下面是上述方法的实现:
C++
// C++ implementation to check whether
// two convex polygons have same center
#include
using namespace std;
// Function to check whether two convex
// polygons have the same center or not
int check(int n, int m){
if (m % n == 0){
cout << "YES";
}
else{
cout << "NO";
}
return 0;
}
// Driver Code
int main()
{
int n = 5;
int m = 10;
check(n, m);
return 0;
}
Java
// Java implementation to check whether
// two convex polygons have same center
class GFG{
// Function to check whether two convex
// polygons have the same center or not
static int check(int n, int m){
if (m % n == 0){
System.out.print("YES");
}
else{
System.out.print("NO");
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int m = 10;
check(n, m);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 implementation to check whether
# two convex polygons have same center
# Function to check whether two convex
# polygons have the same center or not
def check(n, m):
if (m % n == 0):
print("YES")
else:
print("NO")
# Driver Code
n = 5
m = 10
check(n, m)
# This code is contributed by mohit kumar 29
C#
// C# implementation to check whether
// two convex polygons have same center
using System;
class GFG{
// Function to check whether two convex
// polygons have the same center or not
static int check(int n, int m){
if (m % n == 0){
Console.Write("YES");
}
else{
Console.Write("NO");
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
int m = 10;
check(n, m);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
YES
性能分析:
- 时间复杂度: O(1)。
- 辅助空间: O(1)。