728x90
반응형
2024.07.06기준 - 브론즈2
백준, BEAKJOON, BOJ, JAVA, 자바
풀이
이 문제는 행운 수치를 제공된 주문서를 이용하여 최댓값을 출력하는 문제입니다.
주문서의 종류는 2가지가 있습니다.
- 현재 행운 수치에 더해지는 주문서
- 현재 행운 수치에 곱해지는 주문서
저의 접근은
- 곱하는 건 순서에 상관 없이 시작부터 큰 값을 곱해야 한다. (더하기 주문서를 먼저 다 사용한다.)
- 주의 점은 0을 곱하면 0이 되기 때문에 더하기 전에 0을 먼저 수행한다. (0은 어떤 수와 곱해도 0이기 때문에 계산하지 않는다,)
- 계산된 행운 수치를 출력한다.
코드
package Main;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()); // 더해지는 주문서 개수
int m = Integer.parseInt(st.nextToken()); // 곱해지는 주문서 개수
long now = 0;
st = new StringTokenizer(br.readLine());
while (st.hasMoreElements()) {
now += Integer.parseInt(st.nextToken());
}
int num = 0;
st = new StringTokenizer(br.readLine());
while (st.hasMoreElements()) {
num = Integer.parseInt(st.nextToken());
if (num != 0) { // 0일 때는 가장 먼저 0을 곱하고 계산을 하기 때문에 0은 제외한다.
now *= num;
}
}
bw.write(Long.toString(now));
bw.flush();
bw.close();
br.close();
}
}
728x90
반응형
'코딩테스트 일기 (BAEKJOON)' 카테고리의 다른 글
BEAKJOON / 백준 - JAVA 1992번 쿼드트리 (0) | 2024.07.07 |
---|---|
BAEKJOON / 백준 - JAVA 31907번 GIST 찍기 (0) | 2024.07.07 |
BEAKJOON / 백준 - JAVA 27112번 시간 외 근무 멈춰! (0) | 2024.07.06 |
BEAKJOON / 백준 - JAVA 25327번 다중 항목 선호도 조사 (Large) (0) | 2024.07.05 |
BAEKJOON / 백준 - JAVA 1652번 누울 자리를 찾아라 (0) | 2024.07.05 |