문제 풀이

[백준] 10950번: A+B - 3 - JAVA (자바)

auyeol 2023. 1. 20. 12:08
728x90

 

문제

https://www.acmicpc.net/problem/10950

 

10950번: A+B - 3

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

두 정수 A와 B를 입력한 뒤, A+B를 한 결과를 출력하는 문제이다.

 

 

 

 

풀이

 

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 = 0; i<n;i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			System.out.println(a+b);
		}
	}
}

 

정답은 이렇게 해도 맞은걸로 나오지만, 문제에서 제시한 출력 결과와 다르다.

 

 

 

 

 

 

 

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int array[] = new int[n];
		
		for(int i = 0; i<n;i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			array[i] = a+b;
		}
		
		for(int i = 0; i<array.length;i++) {
			System.out.println(array[i]);
		}
	}
}

 

 

 배열을 이용하면 백준과 똑같은 입력, 출력을 할 수 있다.

 

728x90