코딩테스트준비
[백준1546]평균 구하기
예쁜꽃이피었으면
2024. 1. 25. 12:06
https://www.acmicpc.net/problem/1546
1546번: 평균
첫째 줄에 시험 본 과목의 개수 N이 주어진다. 이 값은 1000보다 작거나 같다. 둘째 줄에 세준이의 현재 성적이 주어진다. 이 값은 100보다 작거나 같은 음이 아닌 정수이고, 적어도 하나의 값은 0보
www.acmicpc.net
* 자료형을 int로 잘못적어서 계속 계산이 안됐었다..
+ 개별 점수를 계산해서 다시 합칠 필요없이 한번에 더해서 계산해도 된다고 한다.
-> 수정된 총합 * 100 / 최고점수 / 과목 수
import java.util.Arrays;
import java.util.Scanner;
public class backjoon1546 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
// int[] tempArr = new int[tc];
float[] tempArr = new float[tc];
float maxNum = 0;
for(int i=0; i<tc; i++) {
tempArr[i] = Float.parseFloat(sc.next());
maxNum = Math.max(maxNum,tempArr[i]);
}
// System.out.println(((float)40/(float)80)*100);
float totalSum = 0;
for(int i=0; i<tc; i++) {
tempArr[i] = tempArr[i]/maxNum*100;
totalSum += tempArr[i];
}
// System.out.println(Arrays.toString(tempArr));
System.out.println(totalSum/tc);
}
}
반응형