문제 링크입니다: www.acmicpc.net/problem/14623
14623번: 감정이입
첫 번째 줄에 입력으로 주어진 두 이진수 B1, B2의 곱을 이진수로 출력한다. 출력하는 이진수 앞에 불필요한 0이 붙으면 안 됨에 주의해야 한다. 즉 출력하는 이진수의 시작은 항상 1이어야 한다.
www.acmicpc.net
이진수를 십진수로 변환하는 로직만 안다면 간단히 풀 수 있는 구현 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
long long pow(int p) | |
{ | |
int result = 1; | |
for (int i = 0; i < p; i++) | |
{ | |
result *= 2; | |
} | |
return result; | |
} | |
string convertDecToBinary(long long dec) | |
{ | |
string binary = ""; | |
while (dec) | |
{ | |
binary += dec % 2 + '0'; | |
dec /= 2; | |
} | |
reverse(binary.begin(), binary.end()); | |
return binary; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
string B1, B2; | |
cin >> B1 >> B2; | |
long long B1Dec = 0, B2Dec = 0; | |
int B1Length = B1.length(), B2Length = B2.length(); | |
for (int i = 0; i < B1Length; i++) | |
{ | |
B1Dec += B1[i] == '1' ? pow(B1Length - (i + 1)) : 0; | |
} | |
for (int i = 0; i < B2Length; i++) | |
{ | |
B2Dec += B2[i] == '1' ? pow(B2Length - (i + 1)) : 0; | |
} | |
string result = convertDecToBinary(B1Dec * B2Dec); | |
cout << result << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 14935번 FA (0) | 2021.03.17 |
---|---|
백준 14681번 사분면 고르기 (0) | 2021.03.17 |
백준 14470번 전자레인지 (0) | 2021.03.17 |
백준 14264번 정육각형과 삼각형 (0) | 2021.03.17 |
백준 14173번 Square Pasture (0) | 2021.03.16 |