백준 코딩테스트 단계별로 풀어보기
3단계 반복문 6단계 15552 빠른 A+B 문제입니다
https://www.acmicpc.net/problem/15552
15552번: 빠른 A+B
첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.
www.acmicpc.net
단계별로 풀어보기
단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다
www.acmicpc.net
문제
본격적으로 for문 문제를 풀기 전에 주의해야 할 점이 있다.
입출력 방식이 느리면 여러 줄을 입력받거나 출력할 때 시간초과가 날 수 있다는 점이다.
C++을 사용하고 있고 cin/cout을 사용하고자 한다면, cin.tie(NULL)과 sync_with_stdio(false)를 둘 다 적용해 주고, endl 대신 개행문자(\n)를 쓰자.
단, 이렇게 하면 더 이상 scanf/printf/puts/getchar/putchar 등 C의 입출력 방식을 사용하면 안 된다.
Java를 사용하고 있다면, Scanner와 System.out.println 대신 BufferedReader와 BufferedWriter를 사용할 수 있다.
BufferedWriter.flush는 맨 마지막에 한 번만 하면 된다.
Python을 사용하고 있다면, input 대신 sys.stdin.readline을 사용할 수 있다.
단, 이때는 맨 끝의 개행문자까지 같이 입력받기 때문에 문자열을 저장하고 싶을 경우 .rstrip()을 추가로 해 주는 것이 좋다.
또한 입력과 출력 스트림은 별개이므로, 테스트케이스를 전부 입력받아서 저장한 뒤 전부 출력할 필요는 없다.
테스트케이스를 하나 받은 뒤 하나 출력해도 된다.
자세한 설명 및 다른 언어의 경우는 이 글에 설명되어 있다.이 블로그 글에서 BOJ의 기타 여러 가지 팁을 볼 수 있다.
입력
첫 줄에 테스트케이스의 개수 T가 주어진다.
T는 최대 1,000,000이다.
다음 T줄에는 각각 두 정수 A와 B가 주어진다.
A와 B는 1 이상, 1,000 이하이다.
출력
각 테스트케이스마다 A+B를 한 줄에 하나씩 순서대로 출력한다.
정답
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 IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for (int i=0; i<T; i++) {
String str = br.readLine();
int A = Integer.parseInt(str.split(" ")[0]);
int B = Integer.parseInt(str.split(" ")[1]);
bw.write(A+B+"\n");
}
bw.flush();
br.close();
bw.close();
}
}
=>
BufferedReader와 BufferedWriter로 입출력 받고
T로 반복횟수 받고
정수 A와 B를 반복해서(for) 받아 더함
1-1-2557 Hello World https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 A+B https://developernew.tistory.com/42
1-3-1001 A-B https://developernew.tistory.com/43
1-4-10998 AXB https://developernew.tistory.com/44
1-5-1008 A/B https://developernew.tistory.com/45
1-6-10869 사칙연산 https://developernew.tistory.com/46
1-7-10926 ??! https://developernew.tistory.com/47
1-8-18108 1998년생인 내가 태국에서는 2541년생?! https://developernew.tistory.com/48
1-9-10430 나머지 https://developernew.tistory.com/49
1-10-2588 곱셈 https://developernew.tistory.com/50
1-11-11382 꼬마 정민 https://developernew.tistory.com/51
1-12-10171 고양이 https://developernew.tistory.com/52
1-13-10172 개 https://developernew.tistory.com/53
2-1-1330 두 수 비교하기 https://developernew.tistory.com/54
2-2-9498 시험 성적 https://developernew.tistory.com/55
2-3-2753 윤년 https://developernew.tistory.com/56
2-4-14681 사분면 고르기 https://developernew.tistory.com/61
2-5-2884 알람 시계 https://developernew.tistory.com/68
2-6-2525 오븐 시계 https://developernew.tistory.com/69
2-7-2480 주사위 https://developernew.tistory.com/72
3-1-2739 구구단 https://developernew.tistory.com/73
3-2-10950 A+B - 3 https://developernew.tistory.com/75
3-3-8393 합 https://developernew.tistory.com/77
3-4-25304 영수증 https://developernew.tistory.com/79
3-5-25314 코딩은 체육과목 입니다 https://developernew.tistory.com/82
'코딩테스트 > 백준(JAVA)' 카테고리의 다른 글
백준 코딩테스트 3-8-11022 A+B -8 코딩 문제 java로 푸는 방법 (0) | 2023.03.21 |
---|---|
백준 코딩테스트 3-7-11021 A+B -7 코딩 문제 java로 푸는 방법 (0) | 2023.03.21 |
백준 코딩테스트 3-5-25314 코딩은 체육과목 입니다 코딩 문제 java로 푸는 방법 (0) | 2023.03.20 |
백준 코딩테스트 3-4-25304 영수증 코딩 문제 java로 푸는 방법 (0) | 2023.03.15 |
백준 코딩테스트 3-3-8393 합 코딩 문제 java로 푸는 방법 (0) | 2023.03.14 |