You are viewing a single comment's thread. Return to all comments →
JAVA
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = scanner.nextInt(); while (tests-- > 0) { int target = scanner.nextInt(); int root = (int) Math.sqrt(target); int bestRectangles = 0; int bestArea = 0; for (int x = 1; x <= root + 1; x++) { int y = x; int rectangles = 0; do { int area = x * y; rectangles = x * (x + 1) * y * (y + 1) / 4; if (Math.abs(bestRectangles - target) > Math.abs(rectangles - target)) { bestRectangles = rectangles; bestArea = area; } if (Math.abs(bestRectangles - target) == Math.abs(rectangles - target) && bestArea < area) { bestArea = area; } y++; } while (rectangles < target); if (y == x + 1) { break; } } System.out.println(bestArea); } scanner.close(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #85: Counting rectangles
You are viewing a single comment's thread. Return to all comments →
JAVA