문제
https://www.acmicpc.net/problem/1546
슈도코드
String으로 받아옴 split 배열로 만들고 계산
가장 높은 점수를 찾아야함
모든 값을 /가장높은점수*100으로 계산하고
다 더해서 평균을 구한다.
나의 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCnt = Integer.parseInt(br.readLine());
String scoreStr = String.valueOf(br.readLine());
String[] scoreArr = scoreStr.split(" ");
int maxScore = 0;
for (int i = 0; i < numCnt; i++) {
int score = Integer.parseInt(scoreArr[i]);
if(maxScore < score ){
maxScore = score;
}
}
float newSum = 0;
float avl = 0L;
for (int i = 0; i < numCnt; i++) {
newSum += (Float.parseFloat(scoreArr[i])/maxScore * 100);
}
avl = newSum / numCnt;
System.out.println(avl);
}
}
만난 에러
read로 했을 때 char로 읽어오기 때문에 3 -> 51
다른사람 풀이
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
int n = readInt();
int maxScore = 0;
int totalScore = 0;
for (int i = 0; i < n; ++i) {
int score = readInt();
totalScore += score;
maxScore = maxScore > score ? maxScore : score;
}
System.out.println(100.0d * totalScore / maxScore / n);
}
static int readInt() throws IOException {
int retValue = 0;
boolean negative = false;
while (true) {
int i = System.in.read();
if (i == '\r') continue;
if (i == ' ' || i == '\n') {
break;
}
else {
if (i == '-') negative = true;
else {
retValue = retValue * 10 + i - '0';
}
}
}
return negative ? -1 * retValue : retValue;
}
}
배운 것
read
readLine
https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html
반응형
'코딩 테스트' 카테고리의 다른 글
배열 vs 리스트 (0) | 2024.05.07 |
---|---|
[백준] 11720 숫자의 합 (0) | 2024.05.05 |