반응형
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String str = null;
int[][] numArr = new int[n][];
String[] temp = new String[n];
//receives inputs and saves into temp as string
int esc = 0;
while((str = br.readLine()) != null) {
temp[esc] = str;
esc++;
if(esc == n) break;
}
//converts string to two-dimesional array int
for(int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(temp[i]);
esc = st.countTokens();
int j = 0;
numArr[i] = new int[st.countTokens()];
while(st.hasMoreTokens()) {
numArr[i][j] = Integer.parseInt(st.nextToken());
j++;
}
}
//find avg, compare and print out
for(int i = 0; i < numArr.length; i++) {
double avg = 0;
for(int z = 1; z < numArr[i].length; z++) {
avg += numArr[i][z];
}
avg /= (numArr[i].length - 1);
int count = 0;
for(int z = 1; z < numArr[i].length; z++) {
if(avg < numArr[i][z]) count++;
}
avg = (double)count / (numArr[i].length - 1) * 100;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
알고리즘을 접근할 때 사용자의 입력값을 당연히 저장해야 되는 줄 알았는데 생각해보니 bufferedwriter의 장점이 flush로 나중에 한 번에 처리가 가능하다는 점을 사용을 못 했네요.
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
32
33
34
35
36
37
38
39
40
41
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main{
public static void main(String args[]) throws NumberFormatException, IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int count = Integer.parseInt(br.readLine());
for(int i = 0 ; i < count ; i ++){
String datas [] = br.readLine().split(" ");
double sum = 0;
double avg = 0;
double tempCnt = 0;
for(int j = 1 ; j < datas.length ; j ++){
sum += Double.parseDouble(datas[j]);
}
avg = (sum / (datas.length -1) );
for(int j = 1 ; j < datas.length ; j ++){
if(Double.parseDouble(datas[j]) > avg){
tempCnt++;
}
}
bw.newLine();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
여기처럼 값을 받자마자 바로 처리하고 값을 다시 받는 식으로 하면 제 코드처럼 굳이 2차원 배열에 안 담고 계산하면 되니
오늘도 배워갑니다!
반응형
'IT > 알고리즘' 카테고리의 다른 글
백준 6단계(4673) (0) | 2019.12.07 |
---|---|
백준 6단계(15596) (0) | 2019.12.07 |
백준 5단계(8958) (0) | 2019.11.28 |
백준 5단계(1546) (0) | 2019.11.28 |
백준 5단계(3052) (0) | 2019.11.28 |