본문 바로가기

프로그래머스49

[프로그래머스] 하샤드 수 프로그래머스 1단계 하샤드 수 https://programmers.co.kr/learn/challenges 문제 설명 풀이 class Solution { public boolean solution(int x) { boolean answer = false; int sum = 0; int xValue = x; while(xValue != 0) { sum += xValue % 10; xValue /= 10; } if (x % sum == 0) { answer = true; } return answer; } } 2022. 4. 24.
[프로그래머스] 최대공약수와 최소공배수 프로그래머스 1단계 최대공약수와 최소공배수 https://programmers.co.kr/learn/challenges 문제 설명 풀이 /* 유클리드 호제법 : 두 수를 곱한 뒤 최대공약수로 나눈 값은 최소공배수이다. */ class Solution { public int[] solution(int n, int m) { int[] answer = new int[2]; int big; if(n > m) { big = n; } else { big = m; } while(true) { if(n%big == 0 && m%big == 0) { answer[0] = big; answer[1] = n*m/big; break; } else { big -= 1; } } return answer; } } 2022. 4. 24.
[프로그래머스] K번째 수 프로그래머스 1단계 K번째 수 https://programmers.co.kr/learn/challenges 문제 설명 풀이 import java.util.Arrays; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; int[] number = new int[array.length]; for(int i=0; i 2022. 4. 24.
[프로그래머스] 콜라츠 추측 프로그래머스 1단계 콜라츠 추측 https://programmers.co.kr/learn/challenges 문제 설명 풀이 class Solution { public int solution(double num) { int answer = 0; if(num == 1) { return 0; } while(num != 1) { if(answer == 500) { return -1; } if(num % 2 == 0) { num /= 2; answer++; } else { num = num * 3 + 1; answer++; } System.out.print(num+" "); } return answer; } } 2022. 4. 23.