[BOJ][2588] 곱셈
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/2588
2. 사용 알고리즘
구현
3. 풀이
B의 맨 마지막 수와 A를 곱해가며 계산
단계마다 10을 곱해주고, 곱한 값을 더해가며 마지막 결과로 출력
4. 소스 코드
4-1. C++
https://github.com/dev-aiden/problem-solving/blob/main/boj/2588.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int a, b; cin >> a >> b;
int ans = 0, temp = 1;
while (b != 0) {
int num = a * (b % 10);
cout << num << "\n";
b /= 10;
ans += (num * temp);
temp *= 10;
}
cout << ans << "\n";
return 0;
}
4-2. JAVA
https://github.com/dev-aiden/problem-solving/blob/main/boj/2588.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
int ans = 0, temp = 1;
while (b != 0) {
int num = a * (b % 10);
sb.append(num).append("\n");
b /= 10;
ans += (num * temp);
temp *= 10;
}
sb.append(ans).append("\n");
System.out.println(sb);
}
}
Leave a comment