📍 What is the air speed velocity...
https://www.acmicpc.net/problem/9159
입력의 첫 번째 줄 6 5는 차례대로 African swallows, European swallows이다.
그리고 그 다음줄부터 20개의 입력을 받는데, a나 A 값 10개, e나 E 값 10개를 받는다.
double[] african = new double[10];
double[] european = new double[10];
대문자 소문자 상관없이 같은 African이기 때문에 소문자로 치환한 후 같은 배열에 담는다.
for (int i = 0; i < 20; i++) {
String str = sc.readLine();
char opt = Character.toLowerCase(str.charAt(0));
double time = Double.parseDouble(str.substring(2));
switch (opt) {
case 'a': {
african[africanIdx++] = time;
africanTime += time;
break;
}
case 'e': {
european[europeanIdx++] = time;
europeanTime += time;
break;
}
}
}
Method1의 분자는 처음 입력받았던 수 * 10 (6 * 10)이고,
분모는 a나 A로 입력받은 모든 값들을 더한 값이다.
a나 A로 입력받은 모든 값들을 더한 값은 11.0이고, e나 E는 19.0이다.
private static double Method1(double africanSwallow, double africanTime) {
return africanSwallow * 10 / africanTime;
}
Method2는 처음 입력받았던 수를 a값으로 나누어 더한값을 10으로 나눈 값이다.
private static double Method2(double[] african, double africanSwallow) {
double sumSpeeds = 0;
for (double time : african) {
sumSpeeds += africanSwallow / time;
} return sumSpeeds / african.length;
}
✨ 전체코드 ✨
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(sc.readLine());
StringBuilder sb = new StringBuilder();
int africanSwallow = Integer.parseInt(st.nextToken());
int europeanSwallow = Integer.parseInt(st.nextToken());
double[] african = new double[10];
double[] european = new double[10];
int africanIdx = 0;
int europeanIdx = 0;
double africanTime = 0;
double europeanTime = 0;
for (int i = 0; i < 20; i++) {
String str = sc.readLine();
char opt = Character.toLowerCase(str.charAt(0));
double time = Double.parseDouble(str.substring(2));
switch (opt) {
case 'a': {
african[africanIdx++] = time;
africanTime += time;
break;
}
case 'e': {
european[europeanIdx++] = time;
europeanTime += time;
break;
}
}
}
sb.append("Method 1").append('\n');
sb.append("African: ").append(String.format("%.2f", Method1(africanSwallow, africanTime))).append(" furlongs per hour\n");
sb.append("European: ").append(String.format("%.2f", Method1(europeanSwallow, europeanTime))).append(" furlongs per hour\n");
sb.append("Method 2").append('\n');
sb.append("African: ").append(String.format("%.2f", Method2(african, africanSwallow))).append(" furlongs per hour\n");
sb.append("European: ").append(String.format("%.2f", Method2(european, europeanSwallow))).append(" furlongs per hour\n");
System.out.println(sb);
}
private static double Method1(double swallowDistance, double totalTime) {
return swallowDistance * 10 / totalTime;
}
private static double Method2(double[] times, double swallowDistance) {
double sumSpeeds = 0;
for (double time : times) {
sumSpeeds += swallowDistance / time;
} return sumSpeeds / times.length;
}
}
🌀 성능
- 메모리 : 14,452 KB
- 시간 : 132 ms
글도 너무 길고 입출력도 너무 귀찮았지만
중요한건 꺾이지 않고 그냥 하는 마음이다
'Algorithm > Boj' 카테고리의 다른 글
[JAVA | 백준] #17093 Total Circle (0) | 2024.08.06 |
---|---|
[JAVA | 백준] #5337 웰컴 🤗 (0) | 2024.08.06 |
[JAVA | 백준] #13450 Làszlò Babai (0) | 2024.07.29 |
[JAVA | 백준] #31561 시계탑 ⏰ (0) | 2024.07.29 |
[JAVA | 백준] #18821 홀수와 짝수의 대결 (0) | 2024.07.16 |