본문 바로가기

programming/알고리즘 풀이

백준 4344번 - [C,C++,Python] 1차원배열

반응형
SMALL

C


 

#include<stdio.h>

int main()
{
	int c, n;
    scanf("%d", &c);
   	int i =0, j =0;
    int cnt, sum=0;
    double avg = 0;
    
    for ( i =0; i<c ; i++)
    {
    	scnaf("%d",&n);
        int array[n]={0, };
        avg = 0;
        cnt = 0;
        for( j =0; j<n; j++)
        {
        	scanf("%d",array[j]);
            sum += array[j];
        }
        
        avg = (double)sum/n;
        for( j =0; j< n; j++)
        {
        	if(avg < array[j])
            	cnt++;
        }
        printf("%.3f%%\n",(double)cnt*100/n);
        
    }
	return 0;
}

C++


#include<iostream>
using namespace std;

int main()
{
	int c, n;
    int cnt= 0, j=0 ,i=0 ;
    double avg=0.0;
    int sum =0;
    cin >> c;
    for(i=0 ; i <c ; i++)
    {
    	cin >> n;
        int array[n] = {0 , };
        avg = 0.0;
        cnt = 0;
        sum =0;
        
        for( j =0; j<n ; j++)
        {
        	cin>>array[j];
            sum += array[j];
        }
        avg = (double)sum/n;
        
        for(j=0; j<n ; j++)
        {
        	if(array[j]>avg) cnt++;
        }
        
        cout<<(double)cnt*100/n<< "%"<<endl;
    }
    
return 0;
}

Python


c = int(input())

for i in range(c):
	scores = list (map(int, input().split()))
    avg = sum(scores[1:])/scores[0]
    
    cnt = 0
    for i in scores[1:]:
    	if i > avg:
        	cnt += 1
    per = cnt/scores[0])*100
    print('%.3f'%per + '%')

 

반응형
LIST