给定两个正整数N和M,它们表示凸正多边形的边,其中N
例子:
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)。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。