두 문제는 푸는 과정이 비슷하여 같이 작성한다.
두 문제 모두 세 자리 수의 각각 백의 자리, 십의 자리, 일의 자리 숫자를 구해야 한다.
[2588 곱셈]
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0;
int a = 0;
n = sc.nextInt();
a = sc.nextInt();
int b = a / 100; //100의 자리
int c = a % 100 / 10; //10의 자리
int d = a % 10; //1의 자리
System.out.println(n*d);
System.out.println(n*c);
System.out.println(n*b);
System.out.println(n*a);
}
}
몫을 구하는 /와 나머지를 구하는 %를 사용하여 각각의 100, 10, 1의 자리 수를 구했다.
[2908 상수]
public class P3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 0;
a = sc.nextInt();
b = sc.nextInt();
int c = a / 100; //100의 자리 - 1의자리로 변환
int d = a % 100 / 10 * 10; //10의 자리
int e = a % 10 * 100; //1의 자리 - 100의자리로 변환
int f = c + d + e;
int g = b / 100;
int h = b % 100 / 10 * 10;
int i = b % 10 * 100;
int j = g + h + i;
if(f > j) {
System.out.println(f);
}
else {
System.out.println(j);
}
}
}
연관된 문제는 같이 푸는 게 좋을 것 같아 2588번과 2908번을 추천한다.
'코딩테스트 > 백준' 카테고리의 다른 글
[백준 11718] 그대로 출력하기 JAVA (0) | 2024.10.30 |
---|---|
[백준 10809] 알파벳 찾기 JAVA (0) | 2024.10.29 |
[백준 13241] 최소공배수 JAVA (0) | 2024.01.25 |
[백준 3052] 나머지 JAVA (0) | 2024.01.23 |
[백준 10815] 숫자 카드 JAVA (0) | 2024.01.12 |