We want to put a photo into a square frame whose side is S.
The photo is resized keeping the ratio of width to height, so that its longer side becomes exactly S.
A photo smaller than the frame is enlarged, and a larger one is shrunk.
One line gives S, the photo's width w and height h, separated by single spaces.
If w is greater than or equal to h, the new width is S and the new height is the quotient of h × S divided by w.
Otherwise the new height is S and the new width is the quotient of w × S divided by h.
The quotient is the integer left after dropping the fractional part of the division (no rounding).
Print the new width and the new height, in that order, on one line separated by one space.
Input
1 ≤ S, w, h ≤ 10,000 (integers). The longer side is at most S times the shorter side (so the new shorter side is at least 1)
Output
One line with the new width and new height (two integers)
Example 1 · stdin
1080 4000 3000
stdout
1080 810
Example 2 · stdin
500 600 900
stdout
333 500
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
This is an if-else problem that picks exactly one of two branches. The shapes: Python if w >= h: … else: (the colon and indentation are the syntax), C, C++, Java, C#, JS and TS if (w >= h) { … } else { … }, Craykon if (w >= h) { … } else { … }. If you leave out the condition and assume the width is always the longer side, a portrait photo (600 × 900) becomes 500 × 750 and overflows the frame. The arithmetic inside the branches has traps too. First, multiply before dividing: integer / in C, C++, Java and C# keeps only the quotient, so writing h / w * S turns 3000 / 4000 into 0 first and the answer is 0. Second, the way to keep only the quotient differs. In the C family / between ints does it, but Python needs // (/ prints 810.0), JS/TS Math.floor(h * S / w) and Craykon Mathf.Quot(h * S, w). Rounding, as with Math.round, turns 200 / 3 = 66.67 into 67, which is wrong. The products never exceed 100 million, so a 32-bit int is enough. A square photo (w = h) gives S S whichever branch it takes.
Pythonmain.py · 6 lines
S, w, h = map(int, input().split())# 곱하기를 먼저 하고 //로 몫만 남긴다if w >= h: print(S, h * S // w)else: print(w * S // h, S)
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int S = sc.nextInt(); int w = sc.nextInt(); int h = sc.nextInt(); if (w >= h) { System.out.println(S + " " + h * S / w); } else { System.out.println(w * S / h + " " + S); } }}
C#Main.cs · 20 lines
using System;class Program{ static void Main() { string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int S = int.Parse(p[0]); int w = int.Parse(p[1]); int h = int.Parse(p[2]); if (w >= h) { Console.WriteLine($"{S} {h * S / w}"); } else { Console.WriteLine($"{w * S / h} {S}"); } }}
C++main.cpp · 11 lines
#include <iostream>int main() { int S, w, h; std::cin >> S >> w >> h; if (w >= h) { std::cout << S << " " << h * S / w << "\n"; } else { std::cout << w * S / h << " " << S << "\n"; }}
Cmain.c · 13 lines
#include <stdio.h>int main(void) { int S, w, h; scanf("%d %d %d", &S, &w, &h); /* 곱해도 1억까지라 int로 충분하다 */ if (w >= h) { printf("%d %d\n", S, h * S / w); } else { printf("%d %d\n", w * S / h, S); } return 0;}