[백준] 1546번 평균 풀이 코드 (C/C++/Java 자바/Python 파이썬)

by iamtrueline

풀이

입력받은 배열 내 최댓값을 찾고 주어진 식대로 계산하여 출력하면 됩니다.


코드

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
 
int main () {
    int n, i;
    double all = 0, max = 0;
    double score[1000];
    scanf("%d", &n);
 
    for(i = 0; i < n; i++){
        scanf("%lf", &score[i]);
        if(max < score[i]){
            max = score[i];
        }
    }
     
    for(i = 0; i < n; i++){
        all += score[i]/max*100;
    }
 
    printf("%lf", all/(double)n);
    return 0;
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
 
int main () {
    int n;
    double all = 0, max = 0;
    double score[1000];
    cin>>n;
 
    for(int i = 0; i < n; i++){
        cin>>score[i];
        if(max < score[i]){
            max = score[i];
        }
    }
     
    for(int i = 0; i < n; i++){
        all += score[i]/max*100;
    }
 
    cout<<all/(double)n;
    return 0;
}

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double all = 0, max = 0;
        int n = sc.nextInt();
        double score[] = new double[n];
 
        for(int i = 0; i < n; i++){
            score[i] = sc.nextInt();
            if(max < score[i]){
                max = score[i];
            }
        }
 
        for(int i = 0; i < n; i++){
            all += score[i]/max*100;
        }
        System.out.println(all/(double)n);
    }
}

Python

1
2
3
4
5
6
7
8
9
n = int(input())
score = list(map(int, input().split()))
max = max(score)
all = 0
 
for s in score:
    all += s/max*100;
 
print(all/n)

문제 출처

https://www.acmicpc.net/problem/1546

You may also like

Leave a Comment

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00