# 10950 A+B - 3

* 풀이
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int i = 1; i <= T; i++){
int A = sc.nextInt();
int B = sc.nextInt();
System.out.println(A + B);
}
}
}
처음에 A와 B를 for문 밖에 뒀어서 출력 초과가 떴었다.
반복문 밖에 두면 A, B가 한 번만 입력되고 계속 같은 값만 출력.
매 테스트 케이스마다 새로 입력 받아야 하기 때문에 for문 안에 둔다.
# 8393 합

* 풀이
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int result = 0;
for(int i = 1; i <= n; i++){
result += i;
}
System.out.print(result);
}
}
# 25304 영수증


* 풀이
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int X = sc.nextInt();
int N = sc.nextInt();
int sum = 0;
for(int i = 1; i <= N; i++){
int a = sc.nextInt();
int b = sc.nextInt();
sum += a * b;
}
if(X == sum){
System.out.print("Yes");
} else{
System.out.print("No");
}
}
}
# 25314 코딩은 체육과목 입니다.


* 풀이
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for(int i = 1; i <= N/4; i++){
System.out.print("long ");
}
System.out.print("int");
}
}
막상 풀면 코드 자체는 간단한데 왤케 복잡하게 생각하려고 하는지 모르겠다. 쥐뿔도 모르면서~~!
종이에 써가면서 잘 풀기~
# 15552 빠른 A+B

* 풀이
import java.io.*;
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[] input = br.readLine().split(" ");
int A = Integer.parseInt(input[0]);
int B = Integer.parseInt(input[1]);
bw.write((A + B) + "\n");
}
bw.flush();
bw.close();
br.close();
}
}
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
// 입력을 위한 BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 출력을 위한 BufferedWriter
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
// 테스트 케이스 수 입력
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
// 한 줄 입력 → 공백 기준으로 분리
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
bw.write((A + B) + "\n");
}
// 출력은 한 번에!
bw.flush();
bw.close();
br.close(); // 입력도 닫기 (선택)
}
}
A와 B 사이의 공백을 생각하지 못하고 있어 실행하면 런타임 에러가 떴었다.
split()이나 StringTokenizer 사용해야 함.
=> 이에 대한 설명
'코테 > 백준' 카테고리의 다른 글
| [백준 / java 11] 반복문 > 11022번, 2439번, 10952번, 10951번 (0) | 2025.05.03 |
|---|---|
| [백준 / java 11] 1550번 16진수 (0) | 2025.05.01 |
| [백준 / java 11] 2438번 별 찍기 - 1 (0) | 2025.05.01 |
| [백준 / java 11] 2741번 N 찍기 (0) | 2025.05.01 |
| [백준 / java 11] 반복문 > 2739번 구구단 (0) | 2025.04.29 |