티스토리 뷰
반응형
https://softeer.ai/practice/info.do?eventIdx=1&psProblemId=389&sw_prbl_sbms_sn=18278
Softeer
앗 잠깐만요! 소프티어 이용 중 이 페이지를 보신다면 브라우저의 쿠키 때문인 경우가 많습니다. 접속한 브라우저의 쿠키 삭제(Ctrl + Shift + Del) 후 브라우저를 닫고 다시 접속해주세요. 그래도 안
softeer.ai
[문제]
N명의 학생들의 성적이 학번순서대로 주어졌다.
학번 구간 [A, B]가 주어졌을 때 이 학생들 성적의 평균을 구하는 프로그램을 작성하라.
[풀이]
import java.util.*;
import java.io.*;
public class Main
{
static int N;
static int K;
static int[] S;
static int A, B;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
S = new int[N];
for (int i = 0; i < N; i++)
{
S[i] = sc.nextInt();
}
for (int i = 0; i < K; i++)
{
A = sc.nextInt() - 1;
B = sc.nextInt() - 1;
int nSum = 0;
for (int j = A; j <= B; j++)
{
nSum += S[j];
}
float fAverage = ((float) nSum / (B - A + 1));
System.out.printf("%.2f", fAverage);
System.out.println();
}
sc.close();
}
}
반응형
'ALL' 카테고리의 다른 글
[Softeer/소프티어][Level3]징검다리(Java) (0) | 2023.01.11 |
---|---|
[Softeer/소프티어][Level3]수퍼바이러스(Java) (0) | 2023.01.11 |
[Softeer/소프티어][Level2]GBC(Java) (0) | 2023.01.09 |
[Softeer/소프티어][Level2]바이러스(Java) (0) | 2023.01.09 |
java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication has been compiled by a more recent version of the Java Runtime (class file version 61.0) (0) | 2022.12.04 |