
풀이
C
조건에 맞는 소수점 형태에 유의하며 출력합니다. %.3f 형태로 출력하면 소수점 아래 4번째에서 반올림하여 3번째 자리까지 출력합니다.
C++
조건에 맞는 소수점 형태에 유의하며 출력합니다. cout<<fixed, cout.precision(3) 으로 소수점 아래 4번째에서 반올림하여 3번째 자리까지 출력합니다. cout.precision()은 소수점 위 자리를 포함한 출력의 전체 길이를 정하는 함수이므로, 적용 범위를 소수점 아래만으로 한정하는 cout<<fixed 없이 단독으로 사용하면 원하는 출력이 나오지 않습니다.
Java
조건에 맞는 소수점 형태에 유의하며 출력합니다. String.format(“%.3f”, ) 형태로 출력하면 소수점 아래 4번째에서 반올림하여 3번째 자리까지 출력합니다.
Python
조건에 맞는 소수점 형태에 유의하며 출력합니다. ‘{:.3f}’.format() 형태로 출력하면 소수점 아래 4번째에서 반올림하여 3번째 자리까지 출력합니다.
코드
C
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 | #include <stdio.h> int main() { int n, m, i, j, sum, score[1000]; float avg; scanf ( "%d" , &n); for (i = 0; i < n; i++){ scanf ( "%d" , &m); sum = 0; for (j = 0; j < m; j++){ scanf ( "%d" , &score[j]); sum += score[j]; } avg = ( float )sum/( float )m; sum = 0; for (j = 0; j < m; j++){ if (avg < score[j]){ sum++; } } avg = ( float )sum/( float )m*100; printf ( "%.3f%%\n" , avg); } 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 24 25 26 27 28 29 | #include <iostream> using namespace std; int main() { int n, m, i, j, sum, score[1000]; float avg; cin>>n; for (i = 0; i < n; i++){ cin>>m; sum = 0; for (j = 0; j < m; j++){ cin>>score[j]; sum += score[j]; } avg = ( float )sum/( float )m; sum = 0; for (j = 0; j < m; j++){ if (avg < score[j]){ sum++; } } avg = ( float )sum/( float )m*100; cout << fixed; cout.precision(3); cout<<avg<< '%' <<endl; } return 0; } |
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 27 28 29 30 31 | import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m, sum; float avg; int n = sc.nextInt(); for ( int i = 0 ; i < n; i++){ sum = 0 ; m = sc.nextInt(); int [] score = new int [m]; for ( int j = 0 ; j < m; j++){ int s = sc.nextInt(); score[j] = s; sum += score[j]; } avg = ( float )sum/( float )m; sum = 0 ; for ( int j = 0 ; j < m; j++){ if (avg < score[j]){ sum++; } } avg = ( float )sum/( float )m* 100 ; System.out.println(String.format( "%.3f" , avg)+ "%" ); } } } |
Python
1 2 3 4 5 6 7 8 9 10 11 | n = int ( input ()) for _ in range (n): score = list ( map ( int , input ().split())) avg = sum (score[ 1 :]) / score[ 0 ] sm = 0 for i in score[ 1 :]: if i > avg: sm + = 1 avg = sm / score[ 0 ] * 100 print ( '{:.3f}%' . format (avg)) |