본문 바로가기

알고리즘53

[프로그래머스] 최대공약수와 최소공배수 프로그래머스 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.
[알고리즘] 선택정렬 알고리즘 선택정렬 JAVA 코드 data[10] : 정렬할 숫자가 저장 될 배열 count : 입력 받은 숫자의 개수가 저장 될 변수 i : 정렬 회전 수, 비교 기준 값이 있는 위치를 지정해 주는 변수 j : 비교 대상이 있는 위치를 지정해주는 변수 k : 자료를 교환할 때 사용할 임시 변수 num​ : 출력할 때 배열의 위치를 지정해 주는 변수 for문 public class sort_1 { public static void main(String[] args) { int count,i,j,k,num; int data[] = new int[10]; Scanner scan = new Scanner(System.in); for(count=0; count 2022. 4. 23.