Write a shape interface with an area, and have a rectangle class and a right-triangle class implement it. Then write one generic function that finds the largest item of a list, and use it on a list of integers, a list of words and a list of shapes. If several items are largest, pick the one earliest in the list. The input has five parts: the count n, n integers (one line), the count m, m words (one line), the count k, and k shape lines. A shape line is "rect w h" (a rectangle of width w and height h, area w × h) or "tri b h" (a right triangle with base b and height h, area b × h ÷ 2). Print three lines: the largest integer, the word that comes last in dictionary order, and "number shape_line" for the shape with the largest area (number is its 1-based position in the input, shape_line is as given).
Input
1 ≤ n, m, k ≤ 1000. -1,000,000,000 ≤ integer ≤ 1,000,000,000. Words are 1 to 10 lowercase English letters. 1 ≤ w, h, b ≤ 10,000 (integers)
Output
three lines
Example 1 · stdin
4
9 10 -3 10
3
banana apple cherry
3
rect 2 2
tri 3 3
tri 9 1
stdout
10
cherry
2 tri 3 3
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 · 3 languages
This problem takes only TypeScript, Java and C#, which have interfaces and generics (Python and JS don't write types, so they have no separate generics, and C has neither; C++ templates look very different, so they are left out here). The three languages say "a T that can be compared" differently. Java <T extends Comparable<? super T>> and C# where T : IComparable<T> put a constraint on the type, and the shape interface extends Comparable<Shape> / IComparable<IShape> so that comparing by area is defined once (a Java default method, a C# 8 default interface implementation). Java cannot use List<int>, so it uses List<Integer>. TS numbers and strings have no compare method, so taking a compare function along, as in argMax<T>(items: T[], compare: (a: T, b: T) => number), is natural. Traps: in TS, comparing numbers with < while they are still strings (no Number()) gives string order, like "9" > "10" (with the type left as T the compiler cannot catch it). Computing a triangle's area with integer division b * h / 2 cuts 4.5 down to 4, making it equal to a rectangle of area 4. Comparing twice the area, b * h, keeps everything in integers. To pick the earlier one on equal area, replace only when > 0; >= 0 picks the last one. C# string.CompareTo compares by culture rules (pure code order is string.CompareOrdinal), but this problem uses only lowercase letters, so the result is the same.
TypeScriptmain.ts · 45 lines
import { readFileSync } from "node:fs";interface Shape { twiceArea(): number; // 넓이의 두 배(삼각형도 정수로 비교하려고) describe(): string;}class Rect implements Shape { private w: number; private h: number; constructor(w: number, h: number) { this.w = w; this.h = h; } twiceArea(): number { return 2 * this.w * this.h; } describe(): string { return `rect ${this.w} ${this.h}`; }}class Tri implements Shape { private b: number; private h: number; constructor(b: number, h: number) { this.b = b; this.h = h; } twiceArea(): number { return this.b * this.h; } describe(): string { return `tri ${this.b} ${this.h}`; }}// 가장 큰 것의 자리(같으면 앞의 것). T가 무엇이든 비교 방법만 받으면 된다function argMax<T>(items: T[], compare: (a: T, b: T) => number): number { let best = 0; for (let i = 1; i < items.length; i++) if (compare(items[i], items[best]) > 0) best = i; return best;}const lines: string[] = readFileSync(0, "utf8").split("\n");const nums: number[] = lines[1].trim().split(" ").map(Number);const words: string[] = lines[3].trim().split(" ");const k: number = Number(lines[4]);const shapes: Shape[] = [];for (let i = 0; i < k; i++) { const [kind, a, b] = lines[5 + i].trim().split(" "); shapes.push(kind === "rect" ? new Rect(Number(a), Number(b)) : new Tri(Number(a), Number(b)));}const byOrder = <T>(a: T, b: T): number => (a < b ? -1 : a > b ? 1 : 0);console.log(nums[argMax(nums, (a, b) => a - b)]);console.log(words[argMax(words, byOrder)]);const s = argMax(shapes, (a, b) => a.twiceArea() - b.twiceArea());console.log(`${s + 1} ${shapes[s].describe()}`);
JavaMain.java · 58 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;interface Shape extends Comparable<Shape> { int twiceArea(); // 넓이의 두 배(삼각형도 정수로 비교하려고) String describe(); // 넓이로 비교하는 법을 인터페이스가 한 번만 정한다 default int compareTo(Shape other) { return Integer.compare(twiceArea(), other.twiceArea()); }}class Rect implements Shape { private final int w, h; Rect(int w, int h) { this.w = w; this.h = h; } public int twiceArea() { return 2 * w * h; } public String describe() { return "rect " + w + " " + h; }}class Tri implements Shape { private final int b, h; Tri(int b, int h) { this.b = b; this.h = h; } public int twiceArea() { return b * h; } public String describe() { return "tri " + b + " " + h; }}public class Main { /** 가장 큰 것의 자리(같으면 앞의 것). 서로 비교할 수 있는 T라면 무엇이든. */ static <T extends Comparable<? super T>> int argMax(List<T> items) { int best = 0; for (int i = 1; i < items.size(); i++) if (items.get(i).compareTo(items.get(best)) > 0) best = i; return best; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); List<Integer> nums = new ArrayList<>(); // List<int>는 안 된다 for (String t : in.readLine().trim().split(" ")) nums.add(Integer.parseInt(t)); in.readLine(); List<String> words = List.of(in.readLine().trim().split(" ")); int k = Integer.parseInt(in.readLine().trim()); List<Shape> shapes = new ArrayList<>(); for (int i = 0; i < k; i++) { String[] p = in.readLine().trim().split(" "); int a = Integer.parseInt(p[1]), b = Integer.parseInt(p[2]); shapes.add(p[0].equals("rect") ? new Rect(a, b) : new Tri(a, b)); } System.out.println(nums.get(argMax(nums))); System.out.println(words.get(argMax(words))); int s = argMax(shapes); System.out.println((s + 1) + " " + shapes.get(s).describe()); }}
C#Main.cs · 61 lines
using System;using System.Collections.Generic;using System.Linq;interface IShape : IComparable<IShape>{ int TwiceArea(); // 넓이의 두 배(삼각형도 정수로 비교하려고) string Describe(); // 넓이로 비교하는 법을 인터페이스가 한 번만 정한다(C# 8의 기본 구현) int IComparable<IShape>.CompareTo(IShape other) => TwiceArea().CompareTo(other.TwiceArea());}class Rect : IShape{ private readonly int w, h; public Rect(int w, int h) { this.w = w; this.h = h; } public int TwiceArea() => 2 * w * h; public string Describe() => $"rect {w} {h}";}class Tri : IShape{ private readonly int b, h; public Tri(int b, int h) { this.b = b; this.h = h; } public int TwiceArea() => b * h; public string Describe() => $"tri {b} {h}";}class Program{ // 가장 큰 것의 자리(같으면 앞의 것). 서로 비교할 수 있는 T라면 무엇이든 static int ArgMax<T>(List<T> items) where T : IComparable<T> { int best = 0; for (int i = 1; i < items.Count; i++) if (items[i].CompareTo(items[best]) > 0) best = i; return best; } static void Main() { Console.ReadLine(); var nums = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToList(); Console.ReadLine(); var words = Console.ReadLine().Trim().Split(' ').ToList(); int k = int.Parse(Console.ReadLine()); var shapes = new List<IShape>(); for (int i = 0; i < k; i++) { string[] p = Console.ReadLine().Trim().Split(' '); int a = int.Parse(p[1]), b = int.Parse(p[2]); shapes.Add(p[0] == "rect" ? new Rect(a, b) : new Tri(a, b)); } Console.WriteLine(nums[ArgMax(nums)]); Console.WriteLine(words[ArgMax(words)]); int s = ArgMax(shapes); Console.WriteLine($"{s + 1} {shapes[s].Describe()}"); }}