백준/완전탐색
[백준 1065번 / Java] 한수
ghan2
2024. 5. 14. 16:57
등차수열이 되는 모든 경우의 수를 구하는 문제
한자리, 두자리 일 때에는 모든 수가 등차수열을 이룰 수 있기 때문에 N값을 그대로 출력하면 되고,
1000이 오는 한 가지 경우에는 144를 출력했다.
세자리 수인 경우만 고려하면 됐는데,
111 123 134 146 ... 처럼 i만큼 플러스 한 값, 444 432 420 ...처럼 i만큼 뺀 수열의 경우가 나올 수 있었다.
이 때 111, 222, 333 같은 값이 중복되기 때문에 이 부분을 고려하여 문제를 해결했다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static String N;
static int count = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = br.readLine();
if (N.length() == 1) {
System.out.println(N);
} else if (N.length() == 4) {
System.out.println(144);
}else if (N.length() == 2) {
System.out.println(N);
} else if (N.length() == 3) {
int first = N.charAt(0) - '0';
int second = N.charAt(1) - '0';
int third = N.charAt(2) - '0';
count += 99;
solution(first, second, third);
if(second > first || (second == first && third >= first)) {
count -= first;
} else {
count -= first - 1;
}
System.out.println(count);
}
}
private static void solution(int first, int second, int third) {
for(int i = 1; i <= first; i++) {
for(int j = 0; j <= 4; j++) {
if((i+j+j) <= 9 && (i * 100) + ((i + j) * 10) + (i + j + j) <= Integer.parseInt(N)) {
count++;
}
if((i-j-j)>=0 && i * 100 + (i - j) * 10 + (i - j - j) <= Integer.parseInt(N)) {
count++;
}
}
}
}
}