

-Code
import java.io.*;
import java.util.*;
public class BOJ14721 {
static class StudyFunc {
int x, y;
public StudyFunc(int x, int y) {
this.x = x;
this.y = y;
}
// RSS 계산을 위한 메소드
public long calc(int a, int b) {
long base = this.y - (a * x + b);
return base * base;
}
}
static StudyFunc[] studyInfos;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int dataCnt = Integer.parseInt(br.readLine());
studyInfos = new StudyFunc[dataCnt];
for (int i = 0; i < dataCnt; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
studyInfos[i] = new StudyFunc(x, y);
}
br.close();
long minRSS = Long.MAX_VALUE;
int resA = 0, resB = 0;
for (int a = 1; a <= 100; a++) {
for (int b = 1; b <= 100; b++) {
// 현재 RSS 계산
long nowRSS = 0;
for (int i = 0; i < dataCnt; i++) {
nowRSS += studyInfos[i].calc(a, b);
}
// 현재 최소 RSS보다 작으면 변경 및 A, B 저장
if (minRSS > nowRSS) {
minRSS = nowRSS;
resA = a;
resB = b;
}
}
}
System.out.println(resA + " " + resB);
}
}
최소 RSS의 a, b를 구해야 하는데 주어진 x, y를 기반으로 a, b를 구하는 문제로 착각해서 틀렸었습니다. 해당 부분을 알고 계산식을 확인하여 수정해서 해결했습니다.
'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [백준/Java] 33967번 SCSC 기차 놀이 (0) | 2026.03.08 |
|---|---|
| [백준/Java] 1417번 국회의원 선거 (0) | 2026.03.08 |
| [백준/Java] 10181번 Federation Favorites (0) | 2026.03.07 |
| [백준/Java] 31416번 가상 검증 기술 (0) | 2026.03.06 |
| [백준/Java] 2304번 창고 다각형 (0) | 2026.03.06 |