작성일 :

문제 링크

14623번 - 감정이입

설명

두 이진수를 곱한 결과를 이진수로 출력하는 문제입니다.


접근법

먼저 이진 문자열을 10진 정수로 변환합니다.

다음으로 두 수를 곱한 뒤 다시 이진 문자열로 변환해 출력합니다.

길이 30의 이진수 곱은 최대 2^60 수준이므로 64비트 정수로 처리할 수 있습니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
using System;

class Program {
  static void Main() {
    var b1 = Console.ReadLine()!;
    var b2 = Console.ReadLine()!;
    var n1 = Convert.ToInt64(b1, 2);
    var n2 = Convert.ToInt64(b2, 2);
    var prod = n1 * n2;
    Console.WriteLine(Convert.ToString(prod, 2));
  }
}

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
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string a, b; cin >> a >> b;
  ull n1 = bitset<64>(a).to_ullong();
  ull n2 = bitset<64>(b).to_ullong();
  ull prod = n1 * n2;

  string res;
  if (prod == 0) res = "0";
  else {
    while (prod) {
      res.push_back(char('0' + (prod & 1ULL)));
      prod >>= 1;
    }
    reverse(res.begin(), res.end());
  }

  cout << res << "\n";

  return 0;
}