M个项目将在大小为N的圆中交付。如果我们从给定位置K开始,请找到第M个项目的交付位置。请注意,项目从K开始在相邻的位置分布。
例子 :
Input : N = 5 // Size of circle
M = 2 // Number of items
K = 1 // Starting position
Output : 2
The first item will be given to 1st
position. Second (or last) item will
be delivered to 2nd position
Input : N = 5
M = 8
K = 2
Output : 4
The last item will be delivered to
4th position
我们检查要分配的物品数量是否大于当前循环周期中的剩余位置。如果是,那么我们简单地返回m + k – 1(我们从k开始以相同的周期分配项目)。否则,我们会在完成当前周期后计算剩余商品的数量,并返回剩余商品的mod。
下面是上述想法的实现
C++
// C++ program to find the position where
// last item is delivered.
#include
using namespace std;
// n ==> Size of circle
// m ==> Number of items
// k ==> Initial position
int lastPosition(int n, int m, int k)
{
// n - k + 1 is number of positions
// before we reach beginning of circle
// If m is less than this value, then
// we can simply return (m-1)th position
if (m <= n - k + 1)
return m + k - 1;
// Let us compute remaining items before
// we reach beginning.
m = m - (n - k + 1);
// We compute m % n to skip all complete
// rounds. If we reach end, we return n
// else we return m % n
return (m % n == 0) ? n : (m % n);
}
// Driver code
int main()
{
int n = 5;
int m = 8;
int k = 2;
cout << lastPosition(n, m, k);
return 0;
}
Java
// Java program to find the position where
// last item is delivered.
class GFG {
// n ==> Size of circle
// m ==> Number of items
// k ==> Initial position
static int lastPosition(int n, int m, int k)
{
// n - k + 1 is number of positions
// before we reach beginning of circle
// If m is less than this value, then
// we can simply return (m-1)th position
if (m <= n - k + 1)
return m + k - 1;
// Let us compute remaining items before
// we reach beginning.
m = m - (n - k + 1);
// We compute m % n to skip all complete
// rounds. If we reach end, we return n
// else we return m % n
return (m % n == 0) ? n : (m % n);
}
// Driver Program to test above function
public static void main(String arg[])
{
int n = 5;
int m = 8;
int k = 2;
System.out.print(lastPosition(n, m, k));
}
}
// This code is contributed by Anant Agarwal.
Python
# Python program to find the position where
# last item is delivered.
# n ==> Size of circle
# m ==> Number of items
# k ==> Initial position
def lastPosition(n, m, k):
# n - k + 1 is number of positions
# before we reach beginning of circle
# If m is less than this value, then
# we can simply return (m-1)th position
if (m <= n - k + 1):
return m + k - 1
# Let us compute remaining items before
# we reach beginning.
m = m - (n - k + 1)
# We compute m % n to skip all complete
# rounds. If we reach end, we return n
# else we return m % n
if(m % n == 0):
return n
else:
return m % n
# Driver code
n = 5
m = 8
k = 2
print lastPosition(n, m, k)
# This code is contributed by Sachin Bisht
C#
// C# program to find the position where
// last item is delivered.
using System;
class GFG {
// n ==> Size of circle
// m ==> Number of items
// k ==> Initial position
static int lastPosition(int n, int m, int k)
{
// n - k + 1 is number of positions
// before we reach beginning of circle
// If m is less than this value, then
// we can simply return (m-1)th position
if (m <= n - k + 1)
return m + k - 1;
// Let us compute remaining items before
// we reach beginning.
m = m - (n - k + 1);
// We compute m % n to skip all complete
// rounds. If we reach end, we return n
// else we return m % n
return (m % n == 0) ? n : (m % n);
}
// Driver Program to test above function
public static void Main()
{
int n = 5;
int m = 8;
int k = 2;
Console.WriteLine(lastPosition(n, m, k));
}
}
// This code is contributed by vt_m.
PHP
Size of circle
// m ==> Number of items
// k ==> Initial position
function lastPosition($n, $m, $k)
{
// n - k + 1 is number of
// positions before we reach
// beginning of circle.
// If m is less than this value,
// then we can simply return
// (m-1)th position
if ($m <= $n - $k + 1)
return $m + $k - 1;
// Let us compute remaining items
// before we reach beginning.
$m = $m - ($n - $k + 1);
// We compute m % n to skip
// all complete rounds. If we
// reach end, we return n
// else we return m % n
return ($m % $n == 0) ? $n : ($m % $n);
}
// Driver code
$n = 5;
$m = 8;
$k = 2;
echo lastPosition($n, $m, $k);
// This code is contributed by ajit
?>
Javascript
输出 :
4