给定两个整数N和M ,代表矩形图的长度和宽度,另一个整数K代表人数。每个人将情节分成两部分,使其成为情节中最大的正方形,并将第二部分留给其他人,一直持续到情节结束或每个人都得到情节。现在,任务是确定最后留下的地块面积。
例子:
Input: N = 5, M = 3, K = 2
Output: 2
1st person divides the 5×3 plot into 2 parts i.e 3×3 and 2×3
and will get the plot having dimension 3×3. The other person divides the 2×3 plot again into
two parts i.e 2×2 and 1×2 and will get the plot having dimension 2×2. Now, the remaining
part of plot is having dimension 1×2 and area as 2 units.
Input: N = 4, M = 8, K = 4
Output: 0
方法:
- 如果长度大于宽度,则从长度中减去宽度。
- 如果宽度大于长度,则从宽度中减去长度。
- 当剩余地块的面积大于 0 时,对所有人员重复上述步骤。
- 最后打印剩余图的面积,即长度 * 宽度。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the area
// of the remaining plot
int remainingArea(int N, int M, int K)
{
// Continue while plot has positive area
// and there are persons left
while (K-- && N && M) {
// If length > breadth
// then subtract breadth from length
if (N > M)
N = N - M;
// Else subtract length from breadth
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
// Driver code
int main()
{
int N = 5, M = 3, K = 2;
cout << remainingArea(N, M, K);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the area
// of the remaining plot
static int remainingArea(int N, int M, int K)
{
// Continue while plot has positive area
// and there are persons left
while (K-- > 0 && N > 0 && M > 0) {
// If length > breadth
// then subtract breadth from length
if (N > M)
N = N - M;
// Else subtract length from breadth
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
// Driver code
public static void main(String[] args)
{
int N = 5, M = 3, K = 2;
System.out.println(remainingArea(N, M, K));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
# Function to return the area
# of the remaining plot
def remainingArea(N, M, K):
# Continue while plot has positive area
# and there are persons left
while (K > 0 and N > 0 and M > 0):
# If length > breadth
# then subtract breadth from length
if (N > M):
N = N - M;
# Else subtract length from breadth
else:
M = M - N;
K = K - 1;
if (N > 0 and M > 0):
return N * M;
else:
return 0;
# Driver code
N = 5;
M = 3;
K = 2;
print(remainingArea(N, M, K));
# This code contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the area
// of the remaining plot
static int remainingArea(int N, int M, int K)
{
// Continue while plot has positive area
// and there are persons left
while (K-- > 0 && N > 0 && M > 0) {
// If length > breadth
// then subtract breadth from length
if (N > M)
N = N - M;
// Else subtract length from breadth
else
M = M - N;
}
if (N > 0 && M > 0)
return N * M;
else
return 0;
}
// Driver code
static public void Main()
{
int N = 5, M = 3, K = 2;
Console.WriteLine(remainingArea(N, M, K));
}
}
/* This code contributed by ajit */
PHP
breadth
// then subtract breadth from length
if ($N > $M)
$N = $N - $M;
// Else subtract length from breadth
else
$M = $M - $N;
}
if ($N > 0 && $M > 0)
return $N * $M;
else
return 0;
}
// Driver code
$N = 5;
$M = 3;
$K = 2;
echo remainingArea($N, $M, $K);
// This code is contributed by AnkitRai01
?>
Javascript
输出:
2