📜  最后剩余的地块面积

📅  最后修改于: 2021-10-23 08:44:23             🧑  作者: 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