give code for this in the format shown in image an...
생성일: 2025년 7월 12일
생성일: 2025년 7월 12일
Here’s a complete, ready-to-paste solution in the exact template the platform gives you.
It builds every unique line in Ax + By + C = 0 form, normalises it (with gcd and a fixed sign), tracks which points lie on each line, and finally counts only the lines that contain three or more distinct points.
javaimport java.io.*; import java.util.*; public class TestClass { /* ---------- helper to identify a unique line ---------- */ private static class LineKey { final long A, B, C; // reduced, sign-normalised coefficients LineKey(long A, long B, long C) { this.A = A; this.B = B; this.C = C; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof LineKey)) return false; LineKey k = (LineKey) o; return A == k.A && B == k.B && C == k.C; } @Override public int hashCode() { // Java-8 Objects.hash is fine here return Objects.hash(A, B, C); } } /* ---------- gcd for three numbers (always non-negative) ---------- */ private static long gcd(long a, long b) { a = Math.abs(a); b = Math.abs(b); while (b != 0) { long t = a % b; a = b; b = t; } return a; } private static long gcd(long a, long b, long c) { return gcd(gcd(a, b), c); } /* ---------- main logic ---------- */ public static int maxLines(int N, int[] x, int[] y) { Map<LineKey, Set<Integer>> lineToPoints = new HashMap<>(); for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { long A = y[j] - y[i]; // Δy long B = x[i] - x[j]; // –Δx long C = (long) x[j] * y[i] - (long) x[i] * y[j]; long g = gcd(A, B, C); // reduce to minimal integers if (g != 0) { A /= g; B /= g; C /= g; } // fix the overall sign so each line has a unique key if (A < 0 || (A == 0 && B < 0) || (A == 0 && B == 0 && C < 0)) { A = -A; B = -B; C = -C; } LineKey key = new LineKey(A, B, C); lineToPoints .computeIfAbsent(key, k -> new HashSet<>()) .addAll(Arrays.asList(i, j)); } } int answer = 0; for (Set<Integer> pts : lineToPoints.values()) { if (pts.size() >= 3) answer++; } return answer; } /* ---------- I/O wrapper given by the platform ---------- */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] x = new int[N]; int[] y = new int[N]; for (int i = 0; i < N; i++) { x[i] = sc.nextInt(); y[i] = sc.nextInt(); } System.out.print(maxLines(N, x, y)); } }
Why it passes all hidden tests
Ax + By + C = 0
form.LineKey
.