ZeroJudge D127: Farm Area

Assuming Mr. Z has collected materials just enough to build a fence with a length of L meters. He needs to use this fence to enclose a rectangular pasture. For ease of measurement, it is required that both the length and width of the rectangle must be integers. The problem is how to plan the length and width of the rectangle to maximize the area of the enclosed rectangular pasture.

For example, when L = 14, it's possible to enclose a pasture with a length of 6 and a width of 1, or a length of 4 and a width of 3. Of course, there might be other methods, but the second option yields the largest area, which is 12.

LengthWidthArea
616
4312
The length, width, and area of the pasture when L = 14.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of several lines, each containing an even integer L.Output the maximum area S of the rectangular pasture that can be enclosed with the given L meters of fence.
1412

Thought Process

You can use the following formula to solve this problem, which involves truncating decimals in the calculation. It's important to use a Long Long Int data type to avoid getting a WA.

Sample Code-ZeroJudge D127: Farm Area

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(nullptr);
    long long int N;
    while (cin >> N)
    {
        long long int a = N/4;
        long long int b = N-2*a;
        b /= 2;
        cout << a * b << endl;
    }
}

//ZeroJudge D127
//Dr. SeanXD

Comments