충분히 쌓여가는
백준[Java] 8393번 합 (*문제는 쉽지만 시간초과에 대한 문제! 중요!) 본문
https://www.acmicpc.net/problem/15552
문제의 어려운 점
- 시간제한은 1.5초 즉 1500ms 이내
- Scanner 사용시 시간초과
- System.out.printn 시간초과
문제풀이 1
BufferedWriter 사용
// BufferedWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class _15552_1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
StringTokenizer st;
for (int i = 0; i < T; i++) {
st = new StringTokenizer(br.readLine(), " ");
bw.write(Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken()) + "\n");
}
br.close();
bw.flush();
bw.close();
}
}
문제풀이 2
StringBuilder 사용
// StringBuilder
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class _15552_2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringTokenizer st;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < T; i++) {
st = new StringTokenizer(br.readLine(), " ");
sb.append(Integer.parseInt(st.nextToken()) + Integer.parseInt(st.nextToken())).append("\n");
}
br.close();
System.out.println(sb);
}
}
문제풀이 3
// BufferedWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class _15552_3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
for(int i=0; i<T; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int sum = A+B;
bw.write(sum + "\n");
}
bw.flush();
}
}
문제풀이 4
시간단축 제일 빨리됨
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class _15552_4 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < T; i++) {
String str = br.readLine();
int target = str.indexOf(" ");
int result = Integer.parseInt(str.substring(0, target)) + Integer.parseInt(str.substring(target + 1));
sb.append(result + "\n");
}
br.close();
System.out.print(sb);
}
}
'IT > Java[백준]' 카테고리의 다른 글
백준[Java] 11022번 A+B-8 (0) | 2023.02.20 |
---|---|
백준[Java] 11021번 A+B-7 (0) | 2023.02.20 |
백준[Java] 25304번 영수증 (0) | 2023.02.16 |
백준[Java] 8393번 합 (0) | 2023.02.16 |
백준[Java] 10950번 A+B-3 (0) | 2023.02.15 |