📜  在圆上完全相反的人的位置

📅  最后修改于: 2021-04-29 11:54:59             🧑  作者: Mango

一个圆的圆周上站着n个人。给定一个常识m的位置,任务是找到圆上与m完全相反的人的位置。

一个人在圆周上与他完全相反的位置

例子:

方法:有两种情况:

  • 如果m> n / 2,则答案将始终为m –(n / 2)
  • 如果m≤n / 2,则答案将始终为m +(n / 2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the required position
int getPosition(int n, int m)
{
    if (m > (n / 2))
        return (m - (n / 2));
 
    return (m + (n / 2));
}
 
// Driver code
int main()
{
    int n = 8, m = 5;
    cout << getPosition(n, m);
 
    return 0;
}


Java
// Java implementation of the approach
class Sol
{
 
// Function to return the required position
static int getPosition(int n, int m)
{
    if (m > (n / 2))
        return (m - (n / 2));
 
    return (m + (n / 2));
}
 
// Driver code
public static void main(String args[])
{
    int n = 8, m = 5;
    System.out.println(getPosition(n, m));
 
}
}
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach
 
# Function to return the
# required position
def getPosition(n, m):
 
    if (m > (n // 2)) :
        return (m - (n // 2))
 
    return (m + (n // 2))
 
# Driver code
n = 8
m = 5
print(getPosition(n, m))
 
# This code is contributed
# by ihritik


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the required position
static int getPosition(int n, int m)
{
    if (m > (n / 2))
        return (m - (n / 2));
 
    return (m + (n / 2));
}
 
    // Driver code
    static public void Main ()
    {
         
    int n = 8, m = 5;
    Console.WriteLine(getPosition(n, m));
    }
}
 
// This code is contributed by ajit.


PHP
 ($n / 2))
        return ($m - ($n / 2));
 
    return ($m + ($n / 2));
}
 
// Driver code
$n = 8;
$m = 5;
echo getPosition($n, $m);
 
// This code is contributed
// by ihritik
?>


Javascript


输出:
1

时间复杂度: O(1)