문제
입력
첫째 줄에는 테스트 케이스의 개수 C가 주어진다.
둘째 줄부터 각 테스트 케이스마다 학생의 수 N(1 ≤ N ≤ 1000, N은 정수)이 첫 수로 주어지고, 이어서 N명의 점수가 주어진다. 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.
출력
각 케이스마다 한 줄씩 평균을 넘는 학생들의 비율을 반올림하여 소수점 셋째 자리까지 출력한다.
작성답안
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int man = s.nextInt();
int[] score = new int[s.nextInt()];
double sum = 0;
int excessnum = 0;
for(int i=0; i < score.length; i++) {
score[i] = s.nextInt();
sum = sum + score[i];
}
for(int i=0; i < score.length; i++) {
if(score[i] > sum/score.length ) {
excessnum++;
}
}
System.out.print(excessnum/score.length+"%");
}
}
??? 반올림은 어떻게 해주지??
<답안>
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr;
int testcase = in.nextInt();
for(int i = 0 ; i < testcase ; i++) {
int N = in.nextInt(); //학생 수
arr = new int[N];
double sum = 0; // 성적 누적 합 변수
// 성적 입력부분
for(int j = 0 ; j < N ; j++) {
int val = in.nextInt(); // 성적 입력
arr[j] = val;
sum += val; // 성적 누적 합
}
double mean = (sum / N) ;
double count = 0; // 평균 넘는 학생 수 변수
// 평균 넘는 학생 비율 찾기
for(int j = 0 ; j < N ; j++) {
if(arr[j] > mean) {
count++;
}
}
System.out.printf("%.3f%\n",(count/N)*100);
}
in.close();
}
}
수정보완
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int[] score;
int man = s.nextInt();int[] score = new int[s.nextInt()];
// 선언과 객체의 초기화를 따로 해줘야 한다.
학생수를 다시 출력하게끔 해주고, 그 다음에 점수가 나오도록 해야한다.
//for(int i = 0 ; i < man ; i++) {
int N = in.nextInt(); //학생 수를 위해 여기서 새 변수 N을 생성.
score = new int[N];
}
double sum = 0; // 성적 누적 합 변수
double sum = 0;int excessnum = 0; // double excessnum = 0; 로 수정
for(int i=0; i < score.length; i++) {
score[i] = s.nextInt();
sum = sum + score[i];
}
for(int i=0; i < score.length; i++) {
if(score[i] > sum/score.length ) {
excessnum++;
}
}System.out.print(excessnum/score.length+"%"); // System.out.printf("%.3f%\n",(excessnum/score.length)*100); 로 수정
}
}
'📟java > 백준' 카테고리의 다른 글
백준 1065 자바 (0) | 2022.11.10 |
---|---|
백준 15596 자바 (0) | 2022.11.09 |
백준 1546 자바 (1) | 2022.11.08 |
백준5597 자바 (0) | 2022.11.08 |
[기초]백준 10818 자바 (0) | 2022.11.07 |