728x90
문제
https://www.acmicpc.net/problem/11022
11022번: A+B - 8
각 테스트 케이스마다 "Case #x: A + B = C" 형식으로 출력한다. x는 테스트 케이스 번호이고 1부터 시작하며, C는 A+B이다.
www.acmicpc.net
A+B할 식의 개수(n개)를 정한 뒤, 두 정수 A와 B를 입력, A+B를 한 결과를 출력하는 문제다.
11021번인 A+B에서 출력결과만 바꾼 문제이다.
------------------------------------------------------------------------------------------------------------------
풀이
(1) Scanner 사용
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i;
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int array[] = new int[n];
for(i = 0;i < n;i++)
{
a[i] = sc.nextInt();
b[i] =sc.nextInt();
array[i] = a[i]+b[i];
}
for(i = 0;i < n;i++)
{
System.out.print("Case #");
System.out.print((i+1)+": ");
System.out.println(a[i]+" + "+b[i]+" = "+array[i]);
}
}
}
11021번 문제에서의 출력은 덧셈 결과만 출력하면 돼서 array[] 배열 하나로 설정했었다.
이번 문제는 a와 b의 덧셈 식까지 출력해야 되기 때문에 a와 b 둘 다 배열로 선언한 뒤 출력하였다.
(2) BufferdReader 사용
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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 n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) {
String str = br.readLine();
int a = Integer.parseInt(str.split(" ")[0]);
int b = Integer.parseInt(str.split(" ")[1]);
bw.write("Case #" + (i+1) + ": " + a + " + " + b + " = " + (a+b) + "\n");
}
br.close();
bw.flush();
bw.close();
}
}
BufferedReader는 별로 바뀐게 없다.
728x90
'문제 풀이' 카테고리의 다른 글
[백준] 2439번: 별 찍기 - 2 - JAVA (자바) (0) | 2023.01.31 |
---|---|
[백준] 2438번: 별 찍기 - 1 - JAVA (자바) (0) | 2023.01.27 |
[백준] 11021번: A+B-7 - JAVA (자바) (0) | 2023.01.25 |
[백준] 15552번: 빠른 A+B - JAVA (자바) (0) | 2023.01.23 |
[백준] 25304번: 영수증 - JAVA (자바) (0) | 2023.01.21 |