728x90
반응형
2024.05.22기준 - 브론즈4
백준, BEAKJOON, BOJ, JAVA, 자바
풀이
이 문제는 학식의 가격을 차례로 입력받아 새내기가 원하는 메뉴의 가격의 총액을 출력하는 문제입니다.
먼저 학식의 가격을 입력받아 배열에 저장을 했습니다.
그런 후 새내기의 원하는 메뉴를 입력받아 sum이라는 변수에 총 합을 저장해 출력해 주었습니다.
코드
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));
int n = Integer.parseInt(br.readLine()); // 메뉴의 개수
int[] price = new int[n + 1]; // 메뉴의 가격을 저장하는 배열
for (int i = 1; i <= n; i++) {
price[i] = Integer.parseInt(br.readLine());
}
int sum = 0; // 총 내야될 가격
n = Integer.parseInt(br.readLine()); // 새내기의 인원 수
for (int i = 0; i < n; i++) {
int index = Integer.parseInt(br.readLine()); // 먹고 싶어하는 메뉴
sum += price[index];
}
bw.write(Integer.toString(sum));
bw.flush();
bw.close();
br.close();
}
}
728x90
반응형
'코딩테스트 일기 (BAEKJOON)' 카테고리의 다른 글
BAEKJOON / 백준 - JAVA 31798번 단원평가 (0) | 2024.05.24 |
---|---|
BAEKJOON / 백준 - JAVA 31800번 Best Chance (0) | 2024.05.23 |
BAEKJOON / 백준 - JAVA 31822번 재수강 (0) | 2024.05.21 |
BAEKJOON / 백준 - JAVA 31823번 악질 검거 (2) | 2024.05.20 |
BAEKJOON / 백준 - JAVA 31824번 근로장학생 (0) | 2024.05.19 |