[백준] 1546번 - 평균 [Java][C++]
[백준] 1546번 - 평균 [Java][C++]
1. 문제 풀이
주어진 성적들을 배열로 받은 후 최댓값을 구한다. 이후 각 성적에 대해 점수를 조작한 후 평균을 구하면 된다. 이때 정수 타입은 나머지가 버림 처리 될 수 있음에 주의해야 한다.
2. 코드
1. 사칙연산 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
int max = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
max = Math.max(max, arr[i]);
}
double sum = 0;
for (int num : arr) {
sum += (double) num / max * 100;
}
System.out.println(sum / N);
}
}
2. 사칙연산 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> v(n);
for (int& x : v) cin >> x;
int mx = *max_element(v.begin(), v.end());
double sum = 0;
for (int x : v) {
sum += (double)x / mx * 100;
}
cout << sum / n;
}
3. 풀이 정보
1. 사칙연산 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 112 ms | 14316 KB | 686 B |
2. 사칙연산 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 352 B |
This post is licensed under CC BY 4.0 by the author.