[백준/JAVA]1546번 평균

2025. 1. 13. 15:04·코딩테스트/백준

문제

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

시험 점수를 입력받아 최고 점수를 기준으로 점수를 조정한 후 평균을 구하는 문제였다.

문제 풀이

1. Scanner

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        
        if (N <= 1000) {
            double sum = 0;
            double maxScore = Integer.MIN_VALUE;

            for (int i = 0; i < N; i++) {
                int a = scanner.nextInt();
                if (a <= 100 && a > 0) {
                    sum += a; 
                    if (a > maxScore) {
                        maxScore = a; 
                    }
                }
            }

            double average = (sum / maxScore) * 100 / N;
            System.out.println(average);
        }
        
        scanner.close();
    }
}

2. BufferedReader 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        
        if (N <= 1000) {
            double sum = 0;
            double maxScore = Integer.MIN_VALUE;

            String[] scores = reader.readLine().split(" ");
            for (int i = 0; i < N; i++) {
                int a = Integer.parseInt(scores[i]);
                if (a <= 100 && a > 0) {
                    sum += a; 
                    if (a > maxScore) {
                        maxScore = a; 
                    }
                }
            }

            double average = (sum / maxScore) * 100 / N;
            System.out.println(average);
        }
    }
}

'코딩테스트 > 백준' 카테고리의 다른 글

[백준/JAVA]2743번 단어 길이 재기  (0) 2025.01.14
[백준/JAVA]27866번 문자와 문자열  (0) 2025.01.13
[백준/JAVA]10811번 바구니 뒤집기  (0) 2025.01.13
[백준/JAVA]3052번 나머지  (0) 2025.01.10
[백준/JAVA]5597번 과제 안 내신 분..?  (0) 2025.01.10
'코딩테스트/백준' 카테고리의 다른 글
  • [백준/JAVA]2743번 단어 길이 재기
  • [백준/JAVA]27866번 문자와 문자열
  • [백준/JAVA]10811번 바구니 뒤집기
  • [백준/JAVA]3052번 나머지
yxxjxxeee
yxxjxxeee
  • yxxjxxeee
    공부 일지
    yxxjxxeee
  • 전체
    오늘
    어제
    • study
      • Framework
        • Spring
      • Language
        • JavaScript
        • C | C++
        • JAVA
        • PHP
      • CS
        • 네트워크
        • 자료구조
        • 데이터베이스
        • 운영체제
      • DBMS
        • MySQL
      • 코딩테스트
        • 백준
        • 프로그래머스
        • LeetCode
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 쇼핑몰 제작 프로젝트(PHP)
    • github
  • 공지사항

  • 인기 글

  • 최근 글

  • hELLO Designed By 정상우
    Version v4.10.2
yxxjxxeee
[백준/JAVA]1546번 평균
상단으로

티스토리툴바