728x90
반응형
저주의 숫자 3
/**
* 코딩테스트 입문 Day 22 dp, 수학, 조건문, 배열
* 저주의 숫자 3
*/
public class Main1 {
public static void main(String[] args) {
System.out.println(solution(15));
System.out.println(solution(40));
}
public static int solution(int n) {
int answer = 0;
for (int i = 0; i < n; i++) {
do {
answer++;
} while (answer % 3 == 0 || String.valueOf(answer).contains("3"));
}
return answer;
}
}
평행
/**
* 코딩테스트 입문 Day 22 dp, 수학, 조건문, 배열
* 평행
*/
public class Main2 {
public static void main(String[] args) {
System.out.println(solution(new int[][]{{1, 4}, {9, 2}, {3, 8}, {11, 6}}));
System.out.println(solution(new int[][]{{3, 5}, {4, 1}, {2, 4}, {5, 10}}));
}
public static int solution(int[][] dots) {
int answer = 0;
double slope1 = (double) (dots[1][1] - dots[0][1]) / (double) (dots[1][0] - dots[0][0]);
double slope2 = (double) (dots[3][1] - dots[2][1]) / (double) (dots[3][0] - dots[2][0]);
if (slope1 == slope2) {
answer = 1;
}
slope1 = (double) (dots[2][1] - dots[0][1]) / (double) (dots[2][0] - dots[0][0]);
slope2 = (double) (dots[1][1] - dots[0][1]) / (double) (dots[1][0] - dots[3][0]);
if (slope1 == slope2) {
answer = 1;
}
slope1 = (double) (dots[3][1] - dots[0][1]) / (double) (dots[3][0] - dots[0][0]);
slope2 = (double) (dots[1][1] - dots[2][1]) / (double) (dots[1][0] - dots[2][0]);
if (slope1 == slope2) {
answer = 1;
}
return answer;
}
}
겹치는 선분의 길이
/**
* 코딩테스트 입문 Day 22 dp, 수학, 조건문, 배열
* 겹치는 선분의 길이
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution(new int[][]{{0, 1}, {2, 5}, {3, 9}}));
System.out.println(solution(new int[][]{{-1, 1}, {1, 3}, {3, 9}}));
System.out.println(solution(new int[][]{{0, 5}, {3, 9}, {1, 10}}));
}
public static int solution(int[][] lines) {
int[] cnt = new int[200];
for (int[] line : lines) {
for (int i = line[0] + 100; i < line[1] + 100; i++) {
cnt[i]++;
}
}
int answer = 0;
for (int num : cnt) {
if (num > 1) {
answer++;
}
}
return answer;
}
}
유한소수 판별하기
/**
* 코딩테스트 입문 Day 22 dp, 수학, 조건문, 배열
* 유한소수 판별하기
*/
public class Main4 {
public static void main(String[] args) {
System.out.println(solution(7, 20));
System.out.println(solution(11, 22));
System.out.println(solution(12, 21));
}
public static int solution(int a, int b) {
int tmp = b / GCD(a, b);
while (tmp != 1) {
if (tmp % 2 == 0) {
tmp /= 2;
} else if (tmp % 5 == 0) {
tmp /= 5;
} else {
return 2;
}
}
return 1;
}
public static int GCD (int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
}
728x90
반응형
'📌Back-End > 프로그래머스' 카테고리의 다른 글
Java 프로그래머스 코딩테스트 입문 Day 24 수학, 시뮬레이션, 문자열, 조건문, 반복문 (0) | 2024.07.18 |
---|---|
Java 프로그래머스 코딩테스트 입문 Day 23 배열, 정렬, 문자열 (0) | 2024.07.17 |
Java 프로그래머스 코딩테스트 입문 Day 21 문자열, 사칙연산, 시뮬레이션, 2차원배열, 수학, 배열 (2) | 2024.07.15 |
Java 프로그래머스 코딩테스트 입문 Day 20 수학, 시뮬레이션, 문자열, 사칙연산 (1) | 2024.07.14 |
Java 프로그래머스 코딩테스트 입문 Day 19 문자열, 배열, 조건문 (0) | 2024.07.12 |