[백준 27130] Long Multiplication (C#, C++) - soo:bak
작성일 :
문제 링크
설명
두 정수를 입력받아 긴 곱셈 과정의 중간 결과와 최종 결과를 출력하는 문제입니다.
접근법
첫 번째 수를 그대로 출력한 뒤, 두 번째 수의 각 자릿수에 대해 첫 번째 수와 곱한 결과를 뒤에서부터 순서대로 출력합니다.
마지막에 전체 곱을 출력합니다. 각 정수가 최대 20자리이므로 64비트 정수형으로 표현할 수 없어 문자열로 곱셈을 구현해 처리합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Numerics;
class Program {
static void Main() {
var a = BigInteger.Parse(Console.ReadLine()!);
var b = Console.ReadLine()!;
Console.WriteLine(a);
Console.WriteLine(b);
for (var i = b.Length - 1; i >= 0; i--) {
var d = b[i] - '0';
Console.WriteLine(a * d);
}
Console.WriteLine(a * BigInteger.Parse(b));
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
string mulDigit(const string& a, int d) {
if (d == 0) return "0";
string res;
int carry = 0;
for (int i = (int)a.size() - 1; i >= 0; i--) {
int val = (a[i] - '0') * d + carry;
res.push_back(char('0' + (val % 10)));
carry = val / 10;
}
while (carry > 0) {
res.push_back(char('0' + (carry % 10)));
carry /= 10;
}
reverse(res.begin(), res.end());
return res;
}
string mul(const string& a, const string& b) {
vector<int> res(a.size() + b.size(), 0);
for (int i = (int)a.size() - 1; i >= 0; i--) {
for (int j = (int)b.size() - 1; j >= 0; j--) {
int idx = i + j + 1;
int val = (a[i] - '0') * (b[j] - '0') + res[idx];
res[idx] = val % 10;
res[idx - 1] += val / 10;
}
}
string out;
bool started = false;
for (int v : res) {
if (v != 0 || started) {
out.push_back(char('0' + v));
started = true;
}
}
return started ? out : "0";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a, b; cin >> a >> b;
cout << a << "\n";
cout << b << "\n";
for (int i = (int)b.size() - 1; i >= 0; i--) {
int d = b[i] - '0';
cout << mulDigit(a, d) << "\n";
}
cout << mul(a, b) << "\n";
return 0;
}