본문 바로가기

programming/알고리즘 풀이

백준 1546 -[C/C++/PYTHON] 1차원배열

반응형
SMALL

n = int(input())

for i in range(n):
	score = list(map(int, input().split()))
	m = max(score[0:]))

print(sum(score[0:])/m *100)

C

#include <stdio.h>

int main()
{
	int  n= 0 ,m = 0 ;
	
    scanf("%d", &n);
	int score[n];

	float avg;
    int i;
    
    for( i =0 ; i <n ; i++ )
    {
    	scanf("%d",&score[i]);
    	if( m < score[i]) m = score[i];
    }
    
    for(i = 0 ; i< n ; i++)
    {
    avg += (float)score[i] / m * 100;
    }
    printf("%f\n",avg/n);
return 0;
}

C++

#include <iostream>
using namespace std;

int main()
{
	int n=0 , m =0;
    cin >> n;
    int score[n];
    
    float avg =0 ;
    for(int i = 0 ; i< n ; i ++)
    {
    	cin >> score[i];
        if(m < score[i]) m = score[i];
        
    }
    
    for(int i=0; i< n ; i++)
    {
    	avg += (float)score[i]/m *100;
    }
    
    cout<<(float)avg/n<<endl;
    
    return 0;
}

 

PYTHON

n = int(input())
score = list(map(int, input().split()))
m = max(score)

new_list = []
for i in score:
	new_list.append(i/m*100)
    
print(sum(new_list)/n)

#동일한 방법 더깔끔해짐 
# for i in range(N):
#	score[i] = score[i]/m*100
#
# print( sum(score)/n )
반응형
LIST