Search
Duplicate

05.31 입문 문제풀이

태그
대분류
코딩테스트 입문
비고
영어가 싫어요
package programmers.CodingBegginerTraining; public class PROB30 { public static void main(String[] args) { String numbers = "onetwothreefourfivesixseveneightnine"; System.out.println(solution(numbers)); } public static long solution(String numbers) { String[] str = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; for (int i = 0; i < str.length; i++) { numbers = numbers.replaceAll(str[i], String.valueOf(i)); } return Long.parseLong(numbers); } }
Java
복사
잘라서 배열로 저장하기
package programmers.CodingBegginerTraining; import java.util.*; public class PROB31 { public static void main(String[] args) { String my_str = "abcdef123"; int n = 3; System.out.println(Arrays.toString(solution(my_str, n))); } public static String[] solution (String my_str, int n) { int startIdx = 0; int lastIdx = n; int length; if (my_str.length() % n == 0) { length = my_str.length() / n; } else { length = my_str.length() / n + 1; } String[] answer = new String[length]; for (int i = 0; i < length - 1; i++) { answer[i] = my_str.substring(startIdx, lastIdx); startIdx += n; lastIdx += n; } answer[answer.length - 1] = my_str.substring(startIdx); return answer; } }
Java
복사
문자열 계산하기
package programmers.CodingBegginerTraining; public class PROB32 { public int solution(String my_string) { String[] strArr = my_string.split(" "); int answer = Integer.parseInt(strArr[0]); for (int i = 1; i < strArr.length; i += 2) { if (strArr[i].equals("+")) { answer += Integer.parseInt(strArr[i+1]); } else { answer -= Integer.parseInt(strArr[i+1]); } } return answer; } }package programmers.CodingBegginerTraining; public class PROB33 { public static void main(String[] args) { int balls = 3; int share = 2; System.out.println(solution(balls, share)); } public static int solution(int balls, int share) { return combination(balls, share); } public static int combination(int n, int r) { if( n == r || r == 0) { return 1; } else { return combination(n - 1, r - 1) + combination(n -1, r); } } }
Java
복사
구슬을 나누는 경우의 수
package programmers.CodingBegginerTraining; public class PROB33 { public static void main(String[] args) { int balls = 3; int share = 2; System.out.println(solution(balls, share)); } public static int solution(int balls, int share) { return combination(balls, share); } public static int combination(int n, int r) { if( n == r || r == 0) { return 1; } else { return combination(n - 1, r - 1) + combination(n -1, r); } } }
Java
복사
삼각형의 완성조건 (2)
package programmers.CodingBegginerTraining; public class PROB34 { public static void main(String[] args) { int[] sides = {1, 2}; System.out.println(solution(sides)); } public static int solution(int[] sides) { int max = Math.max(sides[0], sides[1]); int min = Math.min(sides[0], sides[1]); int answer = 0; // 가장 긴변이 max 인 경우 for (int i = max; i < max + min; i++) { answer ++; } // 나머지 한 변이 가장 긴 경우 for (int i = max - min + 1; i < max; i++) { answer ++; } return answer; } }
Java
복사
외계어 사전
package programmers.CodingBegginerTraining; public class PROB35 { public static void main(String[] args) { String[] spell = {"p", "o", "s"}; String[] dic = {"sod", "eocd", "qixm", "adio", "soo"}; System.out.println(solution(spell, dic)); } public static int solution(String[] spell, String[] dic) { int cnt; for (int i = 0; i < dic.length; i++) { cnt = 0; for (int j = 0; j <spell.length; j++) { if (dic[i].contains(spell[j])) { cnt++; } } if (cnt == spell.length) { return 1; } } return 2; } }
Java
복사
종이 자르기
class Solution { public int solution(int M, int N) { return M * N - 1; } }
Java
복사
캐릭터의 좌표
package programmers.CodingBegginerTraining; import java.util.Arrays; public class PROB36 { public static void main(String[] args) { String[] keyinput = {"left", "right", "up", "right", "right"}; int[] board = {11, 11}; System.out.println(Arrays.toString(solution(keyinput, board))); } public static int[] solution(String[] keyinput, int[] board) { int[] current = {0, 0}; int maxWidth = (board[0] - 1) / 2; int minWidth = maxWidth * -1; int maxHeight = (board[1] -1) / 2; int minHeight = maxHeight * -1; for (int i = 0; i < keyinput.length; i++) { if (keyinput[i].equals("left") && current[0] != minWidth) { current[0] -= 1; } else if (keyinput[i].equals("right") && current[0] != maxWidth) { current[0] += 1; } else if (keyinput[i].equals("up") && current[1] != maxHeight) { current[1] += 1; } else if (keyinput[i].equals("down") && current[1] != minHeight) { current[1] -= 1; } } return current; } }
Java
복사