BaekJoon Oline Judge - Step 1, 2
간단히 입력, 출력과 사칙연산 다루기!
import java.util.Scanner;
public class BeakjoonStep1 {
public static void main(String[] args) {
//백준 온라인 저지 1단계
//1000. 두 수 입력받은 후 더하기
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
int num2 = scan.nextInt();
System.out.println(num1+num2);
//10001. 두 수 입력받은 후 빼기
Scanner scan = new Scanner(System.in);
int A = scan.nextInt();
int B = scan.nextInt();
System.out.println(A-B);
//7287. 그대로 출력하기
System.out.println("123");
System.out.println("Your_ICPC_Team_Name");
//10172. 개 출력하기
System.out.println("|\\_/|");
System.out.println("|q p| /}");
System.out.println("( 0 )\"\"\"\\");
System.out.println("|\"^\"` |");
System.out.println("||_/=\\\\__|");
//10718. We love kriii
System.out.println("강한친구 대한육군");
System.out.println("강한친구 대한육군");
//11718. 그대로 출력하기
//next()는 개행문자, 공백문자를 무시하고 입력을 받지만, nextLine은 한줄 단위로 입력받기 떄문에 개행문자도 한 줄로 인식한다.
Scanner scan = new Scanner(System.in);
while(scan.hasNext()) {
String str = scan.next();
System.out.println(str);
}
scan.close();
//11719. 그대로 출력하기 2
//공백과 개행까지 출력해야 한다. => nextLine
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()) {
String str = scan.nextLine();
System.out.println(str);
}
scan.close();
}
}
6번. 설탕배달
이 문제는 5kg 봉투를 최대로 해야하기 때문에 5kg이 최대 들아갈 수 있는 갯수부터 1씩 줄여가면서 3kg을 넣어야 한다.
21kg를 입력받는다고 가정했을 때 5kg 봉투를 최대 4개 만들 수 있다. 그러므로 최대 4부터 5kg 봉투가 하나도 생기지 않는 0까지 줄여나가며 봉투의 갯수를 증가시킨다.
4일 때는 남는 1kg이 발생하므로 i--하여 다시 반복문을 돌린다. 3일때는 5kg 봉투 2개, 그리고 6kg이 남는다. 6을 3으로 나누었을 때 나머지가 0이므로 3kg 봉투 2개가 생겨 총 4개의 봉투에 담을 수 있다.
그리고 봉투의 갯수가 0 이상이라면 최종 봉투의 갯수를, 아니라면 -1를 찍는다.
//6. 설탕 배달
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int num5 = 0;
int num3 = 0;
int res = 0;
//5로 나누어 떨어질 때
if(N % 5 == 0) {
num5 = N / 5;
System.out.println(num5);
}
//5로 나누어 떨어지지 않을 때
else {
for(int i = N/5; i>=0; i--) {
if((N - (5 * i)) % 3 == 0) {
num5 = i;
num3 = (N - (5 * i)) / 3;
break;
}
}
res = num5 + num3;
if(res > 0)
System.out.println(res);
else
System.out.println(-1);
}
'CS > Baekjoon' 카테고리의 다른 글
[Algorithm] 백준 단계별로 풀어보기 - 단계 7. 문자열 (0) | 2019.04.17 |
---|---|
[Algorithm] 백준 단계별로 풀어보기 - 단계 6 (0) | 2019.04.16 |
[Algorithm] 백준 단계별로 풀어보기 - 단계 5 (0) | 2019.04.15 |
[Algorithm] 백준 단계별로 풀어보기 - 단계 4 (0) | 2019.04.07 |
[Algorithm] 백준 단계별로 풀어보기 - 단계 3 (0) | 2019.03.30 |