전체 글
-
1929 - 소수 구하기알고리즘 2023. 5. 24. 20:50
https://www.acmicpc.net/problem/1929 1929번: 소수 구하기 첫째 줄에 자연수 M과 N이 빈 칸을 사이에 두고 주어진다. (1 ≤ M ≤ N ≤ 1,000,000) M이상 N이하의 소수가 하나 이상 있는 입력만 주어진다. www.acmicpc.net 문제요약 M이상 N이하의 소수를 모두 출력 1 ≤ M ≤ N ≤ 1,000,000 import java.io.*; public class Main { public static boolean[] getPrime() { int num = 1000000; boolean[] arr = new boolean[num + 1]; // 1은 소수 아님 arr[1] = true; for (int i = 2; i * i < arr.length; ..
-
10250 - ACM 호텔알고리즘 2023. 5. 24. 20:43
https://www.acmicpc.net/problem/10250 10250번: ACM 호텔 프로그램은 표준 입력에서 입력 데이터를 받는다. 프로그램의 입력은 T 개의 테스트 데이터로 이루어져 있는데 T 는 입력의 맨 첫 줄에 주어진다. 각 테스트 데이터는 한 행으로서 H, W, N, 세 정수 www.acmicpc.net 문제요약 H(높이) W(넓이)와 N(몇번 째 방) 엘레베이터에서 가까운 순으로, 만약 동일한 거리라면, 낮은 층으로 배정 import java.util.Scanner; public class Main { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int T = sc..
-
2869 - 달팽이는 올라가고 싶다알고리즘 2023. 5. 23. 12:21
https://www.acmicpc.net/problem/2869 2869번: 달팽이는 올라가고 싶다 첫째 줄에 세 정수 A, B, V가 공백으로 구분되어서 주어진다. (1 ≤ B < A ≤ V ≤ 1,000,000,000) www.acmicpc.net 문제요약 달팽이가 낮에 A만큼 올라감. V 높이에 도달하면, 더이상 미끄러지지 않는다. 도달못한다면, 밤에 B만큼 미끄러진다. 1 ≤ B < A ≤ V ≤ 1,000,000,000 달팽이가 V 높이까지 올라가는데 걸리는 날을 출력 해당 문제는 단순히 while문을 돌면서 올라간 높이를 계속 누적합하고, 이때의 높이가 V인지를 확인한 후에, 아니라면 B 만큼 빼주고, 하루를 더해주고,... 이런 방법으로는 변수에 해당하는 값을 시간내에 출력하지 못한다. 즉..
-
2839 - 설탕배달알고리즘 2023. 5. 23. 10:32
https://www.acmicpc.net/problem/2839 2839번: 설탕 배달 상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그 www.acmicpc.net 문제요약 특정 무게의 설탕을 3kg, 5kg 봉지로 담을 수 있는 최소 봉지의 갯수 구하기 만약 3kg, 5kg 봉지로 나눌 수 없다면, -1을 출력하기 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextIn..
-
생성자 + 캡슐화의 개념JAVA/클래스 ( 기본 ) 2023. 5. 3. 12:32
생성자란 생성자란 클래스를 통해 인스턴스가 생성될 때, 실행되는 것 목적 인스턴스 생성시 멤버변수 초기화의 용도로 쓰임 생성자도 일종의 메소드와 비슷한데, 따로 호출해주는 것이 아니라 인스턴스가 생성될 때 실행된다. 생성자는 기존의 메소드와는 특이하게 클래스의 이름을 그대로 따라간다. public class Circle { // 속성 -> 변수(필드, 멤버변수) public double radius; // 생성자 public Circle(){ System.out.println("Circle() 생성자 호출"); System.out.println("반지름 값: " + radius); } // 생성자 오버로딩 가능 public Circle(double radius){ System.out.println("Ci..
-
클래스란?JAVA/클래스 ( 기본 ) 2023. 5. 3. 12:12
클래스 vs 인스턴스 우리가 만들고자 하는 대상의 '상태' 와 '기능' 을 함께 가지고 있는 '데이터 타입' 클래스의 사용 클래스를 사용하기 위해서는, 해당 클래스의 인스턴스를 생성해야 한다. 구문 클래스이름 변수이름 = new 생성자(); 생성자(): 클래스의 이름과 동일, 클래스의 멤버변수들을 초기화 클래스 생성 구성요소 상태(속성, 필드) 기능(동작) 클래스는 내가 구현하고자 하는 것이 가지고 있는 상태와 속성을 정의해주고, 이에 따른 기능을 구현하는 방식으로 진행 TV 클래스를 만들어보자 public class MyTV { //상태 private boolean isPowerOn; // 전원 on/off private int channel; // 현재 채널 private int volume; // ..
-
배열 - 2JAVA/배열( array ) 2023. 5. 3. 10:56
구분 내용 생성 시기 / 소멸 시기 Method Area byte code literal final static 생성 : 프로그램 시작 소멸 : 프로그램 종료 Heap 객체(object) 배열... > new로 생성되는 것들 생성 : new 생성시 소멸 : reference가 끊길 때 (Garbage Collection에 의해 자동 소멸) Stack 지역변수 매개변수 생성 : 블럭 안에서 선언될 때 소멸 : 블럭이 끝날 때 배열에 for문 사용과 enhanced for문 사용시의 차이점 기본적으로 두 개의 구문 모두 배열을 순회할 수 있지만, 결정적인 차이가 존재한다 enhanced for문은 기본적으로 배열의 소요를 꺼내어 변수에 담아서 사용하기 때문에 배열의 원소를 직접 변경하는 것이 불가능하다~! ..