📜  确定第三个人在规则N面多边形上的位置

📅  最后修改于: 2021-04-22 00:01:20             🧑  作者: Mango

给定“ N” ,代表常规的N边多边形。两个孩子正站在该正N边多边形的顶点“ A”“ B”上。任务是确定另一个人应该站立的那个顶点的数量,以使到达A所需的最小跳跃与到达B所需的最小跳跃之和最小。

笔记:

  1. 该规则多边形的顶点按顺时针方向从1到N数。
  2. 如果有多个答案,则输出编号最少的顶点。

例子:

Input: N = 6, A = 2, B = 4 
Output: Vertex = 3
Explanation: 
The another person should stand on 3rd vertex. 
As from 3rd vertex,
1 jump is required to reach A 
and 1 jump is required to reach B. 
(See figure above)


Input: N = 4, A = 1, B = 2
Output: Vertex = 3
Explanation: 
The another person should stand on 3rd or 4th vertex. 
But, as mentioned above 
we have to print least numbered vertex
that's why the output is 3.

方法:

  • 只需计算除顶点A和B之外的每个顶点的跳跃,就可以使子代站立在顶点上,并将它们的和存储在sum变量中。
  • 最后,从最小跳数之和处打印该位置。
C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to find out the
// number of that vertices
int vertices(int N, int A, int B)
{
    int position = 0;
    int minisum = INT_MAX;
    int sum = 0;
    for (int i = 1; i <= N; i++) {
 
        // Another person can't stand on
        // vertex on which 2 children stand.
        if (i == A || i == B)
            continue;
 
        // calculating minimum jumps from
        // each vertex.
        else {
 
            int x = abs(i - A);
            int y = abs(i - B);
 
            // Calculate sum of jumps.
            sum = x + y;
 
            if (sum < minisum) {
                minisum = sum;
                position = i;
            }
        }
    }
    return position;
}
 
// Driver code
int main()
{
    int N = 3, A = 1, B = 2;
 
    // Calling function
    cout << "Vertex = " << vertices(N, A, B);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
     
// Function to find out the
// number of that vertices
static int vertices(int N, int A, int B)
{
    int position = 0;
    int minisum = Integer.MAX_VALUE;
    int sum = 0;
    for (int i = 1; i <= N; i++)
    {
 
        // Another person can't stand on
        // vertex on which 2 children stand.
        if (i == A || i == B)
            continue;
 
        // calculating minimum jumps from
        // each vertex.
        else
        {
 
            int x = Math.abs(i - A);
            int y = Math.abs(i - B);
 
            // Calculate sum of jumps.
            sum = x + y;
 
            if (sum < minisum)
            {
                minisum = sum;
                position = i;
            }
        }
    }
    return position;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 3, A = 1, B = 2;
 
    // Calling function
    System.out.println("Vertex = " + vertices(N, A, B));
}
}
 
// This code contributed by Rajput-Ji


Python
# Python3 implementation of above approach
 
# Function to find out the
# number of that vertices
def vertices(N, A, B):
 
    position = 0
    miniSum = 10**9
    Sum = 0
    for i in range(1, N + 1):
 
        # Another person can't stand on
        # vertex on which 2 children stand.
        if (i == A or i == B):
            continue
 
        # calculating minimum jumps from
        # each vertex.
        else:
 
            x = abs(i - A)
            y = abs(i - B)
 
            # Calculate Sum of jumps.
            Sum = x + y
 
            if (Sum < miniSum):
                miniSum = Sum
                position = i
             
    return position
 
 
# Driver code
N = 3
A = 1
B = 2
 
# Calling function
print("Vertex = ",vertices(N, A, B))
 
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
// Function to find out the
// number of that vertices
static int vertices(int N, int A, int B)
{
    int position = 0;
    int minisum = int.MaxValue;
    int sum = 0;
    for (int i = 1; i <= N; i++)
    {
 
        // Another person can't stand on
        // vertex on which 2 children stand.
        if (i == A || i == B)
            continue;
 
        // calculating minimum jumps from
        // each vertex.
        else
        {
 
            int x = Math.Abs(i - A);
            int y = Math.Abs(i - B);
 
            // Calculate sum of jumps.
            sum = x + y;
 
            if (sum < minisum)
            {
                minisum = sum;
                position = i;
            }
        }
    }
    return position;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 3, A = 1, B = 2;
 
    // Calling function
    Console.WriteLine("Vertex = " + vertices(N, A, B));
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:
Vertex = 3