Algorithm

· Algorithm
언어_자바 프로그래머스_주사위 게임 3 주사위 게임 3이 여러 가정을 생각하는 게 어려웠다 😢 다른 사람들 풀이를 보고 이해하고 풀이 없이 코드를 짜는 방식으로 이번에 진행했다. public class DiceGame3 { public static void main(String[] args) { DiceSolution s = new DiceSolution(); int a = 4; int b = 1; int c = 4; int d = 4; s.diceSolution(a,b,c,d); } } class DiceSolution{ public int diceSolution(int a, int b, int c, int d){ int[] dice = {a,b,c,d}; Arrays.sort(dice); int an..
· Algorithm
언어_자바 프로그래머스_문자열 여러 번 뒤집기 class ReverseWord { public String reverseWord(String my_string, int[][] queries) { StringBuilder result = new StringBuilder(my_string); for (int i = 0; i < queries.length; i++) { int start = queries[i][0]; int end = queries[i][1]; StringBuilder str = new StringBuilder(result.substring(start, end + 1)); // subString(처음(포함), 끝(제외)) 이므로 end+1함으로써 end도 포함됨 str.reverse(); re..
· Algorithm
언어_자바 프로그래머스_배열 만들기 2 import java.util.ArrayList; // 프로그래머스에서 에러가 뜬다면 import 했는지 체크! public class MakeArray { public int[] makeArray(int l, int r) { ArrayList result = new ArrayList(); for(int i = l; i
· Algorithm
언어_자바 이것이 코딩 테스트다_92p~95p 큰 수의 법칙 N: 배열의 크기 M: 숫자가 더해지는 횟수 K: 최대 가능한 연속성 횟수 입력 예시 5 8 3 2 4 5 4 6 public class _92BigNum { static Integer n; static Integer m; static Integer k; static Integer first, second; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); ..
· Algorithm
언어_자바 프로그래머스_수 조작하기2 public class Control2 { public static void main(String[] args) { Test5 t = new Test5(); int[] numLog = {0, 1, 0, 10, 0, 1, 0, 10, 0, -1, -2, -1}; t.test5(numLog); } } class Test5{ public String test5(int[] numLog) { StringBuilder result = new StringBuilder(); // StringBuilder 객체 선언 for(int i= 0; i 0){ int diff = numLog[i] - numLog[i-1]; // 두 값의 차이 if(diff == 1){ result.appen..
· Algorithm
언어_자바 프로그래머스_수 조작하기1 public class Control1 { public static void main(String[] args) { Test4 t = new Test4(); int n = 0; String control = "wsdawsdassw"; t.test4(n, control); } } class Test4{ public int test4(int n, String control) { for (char cmd : control.toCharArray()) { // 문자열을 char 배열로 변환 switch (cmd) { case 'w': n += 1; break; case 's': n -= 1; break; case 'd': n += 10; break; case 'a': n -=..
· Algorithm
언어_자바 프로그래머스_마지막 두 원소 public class LastTwo { public static void main(String[] args) { Test3 t = new Test3(); int[] num_list = {5, 2, 1, 7, 5}; t.test3(num_list); } } class Test3{ public int[] test3(int[] num_list) { int[] answer = new int[num_list.length +1]; // 새로운 배열을 생성한다. 중요한 점은 +1 하여 길이를 확장하는 것. int last = 0; if(num_list[num_list.length-1] > num_list[num_list.length-2] ){ last = num_list[nu..
· Algorithm
언어_자바 프로그래머스_이어 붙인 수 public class OddEven { public static void main(String[] args) { Test2 t = new Test2(); int[] num_list = {3,4,5,2,1}; t.test2(num_list); } } class Test2{ public int test2(int[] num_list) { int answer = 0; String odd = ""; String even = ""; for(int i=0; i
· Algorithm
언어_ 자바 프로그래머스_코딩 기초 트레이닝_코드 처리하기 class Solution { public String solution(String code) { String answer =""; int mode = 0; for(int i=0; i += code[i] 조건3: mode 1 && i 가 홀수 -> += code[i] 조건1을 만족하기 위해서 mode 가 0일때는 1이 나와야하고, 1일때는 0이 나와야한다. 따라서 mode = 1 - mode; 로 조건 설정해준다. 조건 2/3을 만족하기 위해서 풀이하자면 아래와 같다. mode가 0일때, i%2 == mode는 곧 i가 짝수이면서 mode 역시 짝수 임을 알 수 있다. 따라서 mode의 값으로 홀/짝을 구분할 수 있기에 else if 한 줄로 코..
31daylee
'Algorithm' 카테고리의 글 목록 (2 Page)