Jakorithm
article thumbnail
728x90

문제

https://softeer.ai/practice/6294

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

 

 

N명의 학생들의 성적이 학번순서대로 주어졌다.

학번 구간 [A, B]가 주어졌을 때 이 학생들 성적의 평균을 구하는 프로그램을 작성하라.

 

제약조건

  • 1 <= N <= 10^6인 정수
  • 1 <= K <= 10^4인 정수
  • 1 <= Si <= 100인 정수
  • 1 <= Ai <= Bi <= N

 

 

입력 예제

5 3
10 50 20 70 100
1 3
3 4
1 5

 

출력 예제

26.67
45.00
50.00

 

 

코드

첫 번째 줄에 학생 수 N과 구간 수 K가 주어진다.

두 번째 줄에는 학생의 성적 Si (1 ≤ i ≤ N)가 주어진다. i + 2 (1 ≤ i ≤ K)번째 줄에는 i번째 구간 Ai, Bi가 주어진다.

 

반복문 사용

import java.io.*;
import java.util.*;

public class Main {
    static int n;
    static int k;
    static int a;
    static int b;
    static int sum;
    static int[] scores;

    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());

        n = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        scores = new int[n];

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            scores[i] = Integer.parseInt(st.nextToken());
        }

        for (int i = 0; i < k; i++) {
            st = new StringTokenizer(br.readLine());
            a = Integer.parseInt(st.nextToken());
            b = Integer.parseInt(st.nextToken());
            sum = 0;

            // 구간 합 구하기
            for (int j = a - 1; j < b; j++) {
                sum += scores[j];
            }

            // 구간 합의 평균을 소수 세 번째 자리에서 반올림하여 출력
            bw.write(String.format("%.2f\n", (double) (sum / (b - a + 1))));
        }


        bw.flush();
        bw.close();
    }
}

 

구간합 사용

import java.io.*;
import java.util.*;

public class Main {
    static int n;
    static int k;
    static int a;
    static int b;
    static int sum;
    static int[] scores;

    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());

        n = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        scores = new int[n];

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            scores[i] = Integer.parseInt(st.nextToken());
        }

        // 구간 합 배열 구하기
        int[] sumScores = new int[n];
        sumScores[0] = scores[0];
        for (int i = 1; i < n; i++) {
            sumScores[i] = sumScores[i - 1] + scores[i];
        }

        for (int i = 0; i < k; i++) {
            st = new StringTokenizer(br.readLine());
            a = Integer.parseInt(st.nextToken()) - 1;
            b = Integer.parseInt(st.nextToken()) - 1;

            // a부터 b까지 더한 값
            if (a == 0) {
                sum = sumScores[b];
            } else {
                sum = sumScores[b] - sumScores[a - 1];
            }

            bw.write(String.format("%.2f\n", (double) sum / (b - a + 1)));
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
728x90