https://www.acmicpc.net/problem/10950
10950번: A+B - 3
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
문제
import java.util.Scanner;
public class Main10950 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int A = 0;
int B = 0;
for (int i=0; i<T; i++) {
A = sc.nextInt();
B = sc.nextInt();
System.out.println(A+B);
}
sc.close();
}
}
이렇게 풀어도 정답이다. 하지만 내가 원했던것은 입력을 한번에 하고 출력도 한번에 되길 원했다.
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int c = in.nextInt();
int arr[] = new int[c];
for (int i = 0; i < c; i++) {
int a = in.nextInt();
int b = in.nextInt();
arr[i] = a + b;
}
in.close();
for (int k : arr) {
System.out.println(k);
}
}
}
배열에 입력연산값을 저장하고 다시 반복문으로 출력값을 만든다. 정답을 맞추긴했지만 반쪽짜리 정답이었다고 생각한다.
'백준' 카테고리의 다른 글
백준 5597번 Java (0) | 2022.10.25 |
---|---|
백준 2438번 220914 (0) | 2022.09.14 |
백준 2480번 220906 (0) | 2022.09.06 |
백준 2884번 220906 (0) | 2022.09.06 |
백준 14681번 220905 (0) | 2022.09.05 |