문제 설명
두 정수 a, b가 주어질 때 다음과 같은 형태의 계산식을 출력하는 코드를 작성해 보세요.
a + b = c
제한사항
1 <= a, b <= 100
입출력 예
입력 #1
4 5
출력 #1
4 + 5 = 9
문제 풀기 전
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
나에겐 쉬운 문제라고 생각했다.
계산식을 출력하는 코드를 작성하라는 문제니까,
변수를 따로 선언해서 결과값을 넣은 다음 System.out.println()에 만들어서 넣어도 되고
System.out.println()에 결과값을 포함해서 넣어도 된다.
가령 c를 새로 만든다고 했을 때 int c = a + b; 를 선언해서 넣어도 된다는 생각이다.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a + b;
System.out.println(a + " + " + b + " = " + c);
}
}
예전에 봤던 printf()를 활용한 풀이법을 사용한 분들이 계셔서 참고삼아 적어둔다.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.printf("%d + %d = %d",a,b,a+b);
}
}
오오..
'코딩테스트' 카테고리의 다른 글
| 문자열 돌리기 LV.0 (1) | 2025.06.06 |
|---|---|
| 문자열 붙여서 출력하기 LV.0 (2) | 2025.06.04 |
| 특수문자 출력하기 LV.0 (0) | 2025.06.04 |
| 대소문자 바꿔서 출력하기 LV.0 (0) | 2025.06.03 |
| 문자열 반복해서 출력하기 LV.0 (0) | 2025.06.02 |