망나니 AWOS의 일상
article thumbnail
[java] 백준 5086번 배수와 약수
알고리즘/백준 2021. 7. 31. 22:19

약수와 배수의 공통점은 나누었을 때 딱 나누어 떨어진다는 점이다. 그래서 두 수의 대소 비교만 해주면 약수와 배수를 구할 수 있을 것이라고 판단하여 아래와 같이 코드를 짰다. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int a = sc.nextInt(); int b = sc.nextInt(); if (a == 0 && b == 0) { break; } if (a b && a % b =..

article thumbnail
[java] 백준 2750번 수 정렬하기
알고리즘/백준 2021. 7. 30. 23:30

문제의 핵심은 오름차순으로 정렬하는 것이다. 이 문장을 보자마자 Arrays.sort()라는 메소드를 이용하면 쉽게 풀 수 있을 것 같아 바로 적용하였다. 기본적으로 오름차순 정렬이기 때문에 간단하게 한줄 써주면 된다. import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; for(int i=0; i

article thumbnail
[java] 백준 10773번 제로
알고리즘/백준 2021. 7. 27. 12:31

해당 문제 같은 경우 Stack 클래스를 사용하면 쉽게 풀 수 있다. Stack은 LIFO구조로 나중에 들어온 값이 먼저 나간다. 힌트에서 볼 수 있는 결과와 같다. push메소드와 pop메소드로 값을 넣고 뺄 수 있다. import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Stack stack = new Stack(); int K = sc.nextInt(); int tot =0; for(int i=0; i

article thumbnail
[java] 백준 2581번 소수
알고리즘/백준 2021. 7. 25. 14:30

문제 조건 1. M이상 N이하의 자연수 중 소수인 것을 모두 찾아 첫째 줄에 합 2. 둘째 줄에 1번 조건의 소수 중 최솟값 출력 3. M이상 N이하의 자연수 중 소수가 없을 경우 첫째 줄에 -1 출력 import java.util.Scanner; // 2581 class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int M = sc.nextInt(); int N = sc.nextInt(); int[] arr = new int[N]; int count, tot=0, sosuCount=0; for(int i=M; i

article thumbnail
[java] 백준 11653번 소인수분해
알고리즘/백준 2021. 7. 24. 12:15

문제 조건 1. 소인수 분해 결과를 한 줄에 하나씩 오름차순으로 출력 (1,2,3 ~~) 2. N이 1인 경우 아무것도 출력 안함 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); if(N == 1) { sc.close(); } for(int i=2; i

article thumbnail
[java] 백준 4153번 직각삼각형
알고리즘/백준 2021. 7. 23. 21:14

마지막에 0 0 0 을 입력시켰을 때 반복문을 종료 시켜주면 되겠다 싶어 이런식으로 구현을 했고 Math.pow 함수를 쓰면 ex) Math.pow(3,2) => 3의 제곱인 9가 나오는 것이다. 그래서 아래와 같이 코드를 작성했다. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true){ int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); // 입력이 0 0 0 일때 반복문 탈출 if(a == 0 && b == 0 && c == 0){ break; } ..

article thumbnail
[java] 백준 1978번 소수 찾기
알고리즘/백준 2021. 7. 22. 21:09

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int sosu=0; for(int i=0; i

article thumbnail
[java] 백준 10757번 큰 수 A+B
알고리즘/백준 2021. 7. 21. 22:01

import java.math.BigInteger; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger A = sc.nextBigInteger(); BigInteger B = sc.nextBigInteger(); System.out.println(A.add(B)); } } int는 메모리 크기는 4byte로 표현할 수 있는 범위는 -2,147,483,648 ~ 2,147,483,647 이고 long은 메모리 크기는 8byte로 표현할 수 있는 범위는 -9,223,372,036,854,775,808 ~ 9,223,372,036,85..

article thumbnail
[java] 백준 2839번 설탕 배달
알고리즘/백준 2021. 7. 20. 22:30

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); if(3

article thumbnail
[java] 백준 2775번 부녀회장이 될테야
알고리즘/백준 2021. 7. 18. 20:15

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int i = 0; i < T; i++) { int k = sc.nextInt(); int n = sc.nextInt(); System.out.println(howManyPeople(k, n)); } } public static int howManyPeople(int k, int n) { // 제한은 1

article thumbnail
[java] 백준 10250번 ACM 호텔
알고리즘/백준 2021. 7. 16. 19:53

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int i=0; i

article thumbnail
[java] 백준 2869번 달팽이는 올라가고 싶다.
알고리즘/백준 2021. 7. 14. 20:09

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); // 낮에 올라갈 수 있는 거리 int b = sc.nextInt(); // 밤에 내려가는 거리 int v = sc.nextInt(); // 나무막대의 높이 int tot = a-b; // 하루에 이동하는 거리 // 정상에 올라간 후에는 미끄러지지 않는다고 했기 때문에 // v에서 a 거리를 먼저 빼주고 계산 v = v - a; // 위에서 마지막 하루 이동하는 거리를 빼고 계산했기 때문에 결과에 하루를 더해줌. // 딱 나누어 떨어질 경우에는 +1 if(v ..

article thumbnail
[java] 백준 1193번 분수 찾기
알고리즘/백준 2021. 7. 12. 20:30

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x= sc.nextInt(); int tmp=0, cross_line=0; // 대각선 줄로 보면 줄하나가 생길때마다 원소가 1개씩 늘어나는 걸 볼 수 있음 // 대각선 줄 구하는 방법 for(int i=1; i= x) { cross_line=i; break; } } // 대각선 줄이 홀수/짝수 개수일 때 해당하는 원소 값 구하는 방법 if(cross_line % 2 == 1) System.out.println((1+tmp-x) + "/" + (cross_line-(tmp-x))); else..

article thumbnail
[java] 백준 2292번 벌집
알고리즘/백준 2021. 7. 10. 20:50

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 1; // 칸수 세는 count int range = 1; // 범위는 1부터 시작 int increase = 1; // 6의 배수만큼 범위를 올리기위해서 사용 // // 범위 // // 1 -> 1 1 1 // // 2~7 -> 2 1 6 // // 8~19 -> 3 6 12 // // 20~37 -> 4 12 18 // // 38~61 -> 5 18 24 while (range < n) { increase = (count++) * 6;..