여기서 중요한 점은 sc.nextLine(); 은 한 줄 전부를 입력으로 받고 sc.nextInt(); 는 숫자를 띄워쓰기나 엔터 전까지 입력을 받는다.

두 가지 코드를 만들어봤다.

첫 번째는 sc.nextInt(); 만으로 간단하게 숫자들을 배열에 입력받아서 결과값을 얻는 것이고

두 번째는 sc.nextLine(); 으로 spilt를 사용해 결과값을 얻는 것이다.

첫 번째 코드

import java.util.Scanner;
public class test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
int n, total, count;
double avg;
int scores[] = new int[1000];
for (int i = 0; i < c; ++i) {
n = sc.nextInt();
total = 0;
count = 0;
for (int j = 0; j < n; ++j) {
scores[j] = sc.nextInt();
total += scores[j];
}
avg = (double)total / n;
for (int j = 0; j < n; ++j) {
if (scores[j] > avg) {
count++;
}
}
System.out.printf("%.3f", 100.0 * count / n);
System.out.println("%");
}
sc.close();
}
}
view raw test.java hosted with ❤ by GitHub

두 번째 코드

import java.util.Scanner;
public class Main{
public static void main(String args[]) {
int person = 0; //몇명의 학생의 성적을 입력 받을지 수를 입력
int grade = 0; //학생들의 성적의 합
int loop =0; //첫번째 입력 받은 수를 그 수만큼 실행하기 위해 사용
float pa = 0; // pa = person average
float result = 0;
Scanner sc = new Scanner(System.in);
int cnt = sc.nextInt();
for(int i =0; i< cnt; ++i) {
String line2 = sc.nextLine();
String[] sp = line2.split(" ");
float cnt2 = Integer.parseInt(sp[0]);
for(int j=0; j<cnt2+1; j++) {
if(j==0) {
person = Integer.parseInt(sp[j]);
}else if(j<=cnt2+1){
grade = grade +Integer.parseInt(sp[j]);
}
}
float a = grade/cnt2;
for(int j=0; j<cnt2+1; j++) {
if(j==0) {
}else if(j<=cnt2+1) {
if(Integer.parseInt(sp[j])>a) {
pa = pa+1;
}
}
}
result = (pa/cnt2)*100;
System.out.printf("%.3f",result);
System.out.println("%");
grade=0;
pa=0;
}
sc.close();
}
}
view raw test.java hosted with ❤ by GitHub


세 번째 코드


'백준 알고리즘 > JAVA' 카테고리의 다른 글

[JAVA 자바] 백준 2577번 숫자의 개수  (0) 2018.07.08
[JAVA 자바]백준 1110번  (0) 2018.07.07
[JAVA 자바] 백준 1546번  (0) 2018.07.05
[JAVA 자바] 백준 10871번  (0) 2018.07.05
[JAVA 자바] 백준 10817번  (0) 2018.07.05

+ Recent posts