cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2011 · EasyUnit conversion

Rectangle Dimensions

Autumn Tournament (2011)

Submission name: rectangle.cpp

Problem summary

A rectangle has perimeter x metres and y centimetres. Its longer side exceeds its shorter side by d centimetres. Recover both side lengths.

Source clarification. The PDF does not explicitly prescribe the order of the two sides; this solution follows its sample by printing the longer side first.

Input

Integers x and y on the first line; integer d on the second line.

Output

Print the longer side, then the shorter side, each on its own line as metres and remaining centimetres.

Constraints

The sought dimensions are no greater than 999,999,999 metres. The statement supplies no further numeric bounds; the data describe a rectangle.

Worked example

Original statement sample, translated without changing its numeric data.

Input

11 40
70

Output

3 20
2 50

Solution analysis

  1. Convert the entire perimeter into centimetres: P = 100x + y. If the sides are L and S, then L + S = P/2 and L − S = d.
  2. Adding the two equations gives L = (P/2 + d)/2. Then S = P/2 − L. Compute in long long, since even a valid side can have almost 10^11 centimetres.
  3. For perimeter 11 m 40 cm, the half-perimeter is 570 cm. With difference 70 cm, the sides are 320 cm and 250 cm, printed as 3 20 and 2 50.

Why it works

The computed lengths have sum P/2 and difference d, so their perimeter and difference match the given data. These two equations have a unique solution, hence they recover the original dimensions.

Complexity: O(1) time and O(1) extra space: the input has a fixed number of values.

Watch out. Convert metres to centimetres before combining units. Split a length with /100 and %100; use long long for the perimeter.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    long long metres,centimetres,difference;
    cin>>metres>>centimetres>>difference;
    long long halfPerimeter=(100*metres+centimetres)/2;
    long long longer=(halfPerimeter+difference)/2;
    long long shorter=halfPerimeter-longer;
    // Quotient and remainder recover metres and centimetres.
    cout<<longer/100<<' '<<longer%100<<'\n';
    cout<<shorter/100<<' '<<shorter%100<<'\n';
}

Source: Arena Olimpiici ↗