https://st-lab.tistory.com/39?category=832565
[백준] 10952번 : A+B - 5 -JAVA [자바]
https://www.acmicpc.net/problem/1001 1001번: A-B 두 정수 A와 B를 입력받은 다음, A-B를 출력하는 프로그램을 작성하시오. www.acmicpc.net 문제 매우 간단한 문제다! ※ 주의할 점 두 정수는 공백으로 구분되..
st-lab.tistory.com
*해당 포스팅은 상단에 링크된 포스팅을 바탕으로 개인 공부 목적을 위해 작성되었으므로 자세한 내용은 위 링크를 확인해 주시기 바랍니다.
제출 코드
import java.beans.beancontext.BeanContext;
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
while(true) {
st = new StringTokenizer(br.readLine(), " ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
if (A == 0 && B == 0) {
break;
}
sb.append((A + B)).append("\n");
}
br.close();
System.out.println(sb);
}
}
다음 코드는 이미 우리는 공백의 자리를 알기 때문에 StringTokenizer를 반복적으로 사용해서 메모리를 많이 사용하는 것보다 charAt을 사용하는 게 더 효율적이라는 설명에 charAt을 사용했다.
변경한 코드
import java.beans.beancontext.BeanContext;
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while(true) {
String str = br.readLine();
int A = str.charAt(0) - '0';
int B = str.charAt(2) - '0';
if (A == 0 && B == 0) {
break;
}
sb.append((A + B)).append("\n");
}
br.close();
System.out.println(sb);
}
}
'Computer Science > Algorithm' 카테고리의 다른 글
[JAVA] 최소, 최대 (0) | 2022.03.04 |
---|---|
[JAVA] A+B - 4 (0) | 2022.03.03 |
[JAVA] X보다 작은 수 (0) | 2022.03.03 |
[JAVA] 빠른 A+B (0) | 2022.03.02 |
[JAVA] 주사위 세 개 (1) | 2022.03.01 |