728x90
반응형
간단한 논리 연산
/**
* 코딩 기초 트레이닝 Day 8
* 간단한 논리 연산
*/
public class Main1 {
public static void main(String[] args) {
System.out.println(solution(false, true, true, true));
System.out.println(solution(true, false, false, false));
}
public static boolean solution(boolean x1, boolean x2, boolean x3, boolean x4) {
return (x1 || x2) && (x3 || x4);
}
}
주사위 게임 3
import java.util.HashMap;
import java.util.Map;
/**
* 코딩 기초 트레이닝 Day 8
* 주사위 게임 3
*/
public class Main2 {
public static void main(String[] args) {
System.out.println(solution(2, 2, 2, 2));
System.out.println(solution(4, 1, 4, 4));
System.out.println(solution(6, 3, 3, 6));
System.out.println(solution(2, 5, 2, 6));
System.out.println(solution(6, 4, 2, 5));
}
public static int solution(int a, int b, int c, int d) {
int answer = 0;
HashMap<Integer, Integer> map = new HashMap<>();
map.put(a, map.getOrDefault(a, 0) + 1);
map.put(b, map.getOrDefault(b, 0) + 1);
map.put(c, map.getOrDefault(c, 0) + 1);
map.put(d, map.getOrDefault(d, 0) + 1);
if (map.size() == 1) { // 네 주사위에서 나온 숫자가 모두 같을 때
answer = a * 1111;
} else if (map.size() == 2) {
if (map.containsValue(3)) { // 세 주사위에서 나온 숫자가 p로 같고, 다른 주사위에서 나온 숫자가 q일 때
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
answer += e.getKey() * (e.getValue() == 3 ? 10 : 1);
}
answer = answer * answer;
} else if (map.containsValue(2)) { // 두 주사위에서 나온 숫자가 p로 같고, 두 주사위에서 나온 숫자가 q로 같을 때
int p = a;
int q = 0;
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
if (e.getKey() != p) {
q = e.getKey();
}
}
answer = (p + q) * Math.abs(p - q);
}
} else if (map.size() == 3) { // 두 주사위에서 나온 숫자가 p로 같고, 두 주사위에서 나온 숫자가 각각 q와 r일 때
answer = 1;
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
if (e.getValue() == 1) {
answer *= e.getKey();
}
}
} else if (map.size() == 4) { // 네 주사위에서 나온 숫자가 모두 다를 때
answer = Math.min(Math.min(a, b), Math.min(c, d));
}
return answer;
}
}
글자 이어 붙여 문자열 만들기
/**
* 코딩 기초 트레이닝 Day 8
* 글자 이어 붙여 문자열 만들기
*/
public class Main3 {
public static void main(String[] args) {
System.out.println(solution("cvsgiorszzzmrpaqpe", new int[]{16, 6, 5, 3, 12, 14, 11, 11, 17, 12, 7}));
System.out.println(solution("zpiaz", new int[]{1, 2, 0, 0, 3}));
}
public static String solution(String my_string, int[] index_list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < index_list.length; i++) {
sb.append(my_string.charAt(index_list[i]));
}
return sb.toString();
}
}
9로 나눈 나머지
/**
* 코딩 기초 트레이닝 Day 8
* 9로 나눈 나머지
*/
public class Main4 {
public static void main(String[] args) {
System.out.println(solution("123"));
System.out.println(solution("78720646226947352489"));
}
public static int solution(String number) {
int answer = 0;
for (char c : number.toCharArray()) {
answer += (c - '0');
}
return answer % 9;
}
}
문자열 여러 번 뒤집기
/**
* 코딩 기초 트레이닝 Day 8
* 문자열 여러 번 뒤집기
*/
public class Main5 {
public static void main(String[] args) {
System.out.println(solution("rermgorpsam", new int[][]{{2, 3}, {0, 7}, {5, 9}, {6, 10}}));
}
public static String solution(String my_string, int[][] queries) {
StringBuilder answer = new StringBuilder();
answer.append(my_string);
for (int i = 0; i < queries.length; i++) {
StringBuilder sb = new StringBuilder();
int s = queries[i][0];
int e = queries[i][1];
String tmp = answer.substring(s, e + 1);
sb.append(tmp).reverse();
answer.replace(s, e + 1, sb.toString());
}
return answer.toString();
}
}
728x90
반응형
'📌Back-End > 프로그래머스' 카테고리의 다른 글
Java 프로그래머스 코딩 기초 트레이닝 Day 10 문자열 (0) | 2024.05.09 |
---|---|
Java 프로그래머스 코딩 기초 트레이닝 Day 9 문자열 (0) | 2024.05.05 |
Java 프로그래머스 코딩 기초 트레이닝 Day 7 반복문 (0) | 2024.05.02 |
Java 프로그래머스 코딩 기초 트레이닝 Day 6 조건문, 반복문 (0) | 2024.05.01 |
Java 프로그래머스 코딩 기초 트레이닝 Day 5 조건문 (0) | 2024.04.30 |