-
백준 1546번 평균Programming/BaekJoon 2021. 8. 11. 04:03
방법은 다양한데 공통적인 풀이는 다음과 같다.
개수를 입력받는다.
점수를 입력받는다.
점수의 최대값을 구한다.
점수 총합을 구한다.
점수를 조작(점수/최대값*100)한 평균을 출력한다.
(※ 이때 점수 조작은 모든 점수에 가해지므로(?) 개별 점수를 조작할 필요 없이
점수의 총합에 주어진 조작을 가하면 된다.)
방법1
배열을 사용하지 않고 StringTokenizer를 이용
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class 평균2 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); br.close(); int max = 0; double sum = 0.0; for(int i = 0; i < N; i++) { int score = Integer.parseInt(st.nextToken()); if(score > max) { max = score; } sum += score; } System.out.println(sum/max*100/N); } }
방법2
배열을 이용
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class 평균1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); // 과목 개수 StringTokenizer st = new StringTokenizer(br.readLine());// 점수를 입력받는다. br.close(); double[] score = new double[N]; double max = 0; //최대점수 구하기 double sum = 0; for (int i = 0; i < N; i++) { score[i] = Integer.parseInt(st.nextToken()); if (max < score[i]) { max = score[i]; } sum += score[i]; } System.out.println(sum/max*100/N); } }
'Programming > BaekJoon' 카테고리의 다른 글
백준 8958번 OX퀴즈 (0) 2021.10.14 백준 11654번 아스키코드 (0) 2021.08.20 백준 3052번 나머지 (0) 2021.07.16 백준 2577번 숫자의 개수 (0) 2021.07.16 백준 2562번 최대값 (0) 2021.07.16