📜  剩余的地块面积

📅  最后修改于: 2021-04-21 20:56:10             🧑  作者: Mango

给定两个整数NM ,它们分别代表矩形图的长度和宽度,另一个整数K代表人数。每个人都将地块分为两部分,以使其成为地块中最大的正方形,并将第二部分留给其他人使用,一直持续到地块结束或每个人都得到地块为止。现在,任务是确定最后剩下的绘图区域。
例子:

方法:

  1. 如果“长度”大于宽度,则从长度中减去宽度。
  2. 如果宽度大于长度,则从宽度中减去长度。
  3. 当剩余图的面积大于0时,对所有人重复上述步骤。
  4. 最后打印剩余图的面积,即长度*宽度。

下面是上述方法的实现:

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