코딩테스트/백준

[코테] 백준 29807 학번을 찾아줘 (java)

developer of the night sky 2023. 9. 22. 00:09

https://www.acmicpc.net/problem/29807

 

29807번: 학번을 찾아줘!

첫째 줄에 김한양이 응시한 과목 수를 나타내는 정수 $T (1 \leq T \leq 5)$가 주어진다. 둘째 줄에 각 과목의 표준점수를 나타내는 $T$개의 정수가 공백으로 구분되어 주어진다. 점수는 국어, 수학, 영

www.acmicpc.net

 

❓문제

 

간단한 문제이지만 자꾸 런타임 에러가 난다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws Exception {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		int num =0;
		int[] score = new int[5];	//입력한 성적을 담는 array
		int[] arr = new int[n];		//각 단계마다 계산한 점수를 담는 array
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		for (int i=0; i<n; i++) {
			score[i] = Integer.parseInt(st.nextToken());
		}
		
		if (score[0]>score[2]) {
			arr[0] = (score[0]-score[2])*508;
		} else {
			arr[0] = Math.abs(score[0]-score[2])*108;
		}
		
		if (score[1] > score[3]) {
			arr[1] = (score[1]-score[3])*212;
		} else {
			arr[1] = (score[3]-score[1])*305;
		}
		
		if (score[4]>0) {
			arr[2] = score[4] * 707;
		}
		
		for (int i=0; i<arr.length; i++) {
			num += arr[i];
		}
		
		num *= 4763;
		
		System.out.println(num);
		
	}
}

 

근데 어이없게 계산할 점수를 arr에 담는 것을 바로 점수 변수인 num에 넣었더니 에러가 해결되었다.

 

⭕정답 코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws Exception {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		int num =0;
		int[] score = new int[5];	//입력한 성적을 담는 array
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		for (int i=0; i<n; i++) {
			score[i] = Integer.parseInt(st.nextToken());
		}
		
		if (score[0]>score[2]) {
			num += (score[0]-score[2])*508;
		} else {
			num += Math.abs(score[0]-score[2])*108;
		}
		
		if (score[1] > score[3]) {
			num += (score[1]-score[3])*212;
		} else {
			num += (score[3]-score[1])*305;
		}
		
		if (score[4]>0) {
			num += score[4] * 707;
		}
		
		num *= 4763;
		
		System.out.println(num);
		
	}
}

 

❗결과