알고리즘 단련장/소프티어

[소프티어] 성적 평균 자바 풀이 레벨3

snapcoder 2024. 7. 19. 20:25
728x90
반응형
SMALL

[소프티어] 성적 평균 자바 풀이 레벨3

 

왜 3레벨인지 모르겠는 문제.

https://softeer.ai/practice/6294/history?questionType=ALGORITHM

 

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

 

softeer.ai

 

 

 

 

핵심코드

String ff = String.format("%.2f", sum/(b-a+1));
            bw.write(String.valueOf(ff)+"\n");

 

 

 

전체코드

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

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());

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

        for(int i=0; i<k; i++){
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken())-1;
            int b = Integer.parseInt(st.nextToken())-1;
            Float sum = 0F;
            for(int j=a; j<=b; j++){
                sum += cost[j];
            }
            String ff = String.format("%.2f", sum/(b-a+1));
            bw.write(String.valueOf(ff)+"\n");
            
        }
        
        
        // bw.write(String.valueOf(dap));
        bw.close();
    }
}
728x90
반응형
LIST