반응형
✏️ 문제 링크
https://www.acmicpc.net/problem/12813
12813번: 이진수 연산
총 100,000 비트로 이루어진 이진수 A와 B가 주어진다. 이때, A & B, A | B, A ^ B, ~A, ~B를 한 값을 출력하는 프로그램을 작성하시오.
www.acmicpc.net
✏️ 문제 설명 (더보기 클릭 👆🏻)
✏️ 문제 풀이
string으로 받아서 각 문자열에 대해서 and, or, xor 연산을 진행해준다.
✏️ 문제 코드
#include <bits/stdc++.h>
using namespace std;
int main(){
string a, b;
cin>>a>>b;
//and
for(int i=0; i<a.size(); i++){
if(a[i]=='1' and b[i]=='1') cout<<1;
else cout<<0;
}
cout<<"\n";
//or
for(int i=0; i<a.size(); i++){
if(a[i]=='1' or b[i]=='1') cout<<1;
else cout<<0;
}
cout<<"\n";
//xor
for(int i=0; i<a.size(); i++){
if((a[i]=='1' and b[i]=='0') or (a[i]=='0' and b[i]=='1') ) cout<<1;
else cout<<0;
}
cout<<"\n";
//reverse A
for(int i=0; i<a.size(); i++){
if(a[i]=='1')cout<<0;
else cout<<1;
}
cout<<"\n";
//reverse B
for(int i=0; i<a.size(); i++){
if(b[i]=='1')cout<<0;
else cout<<1;
}
}
⭐ if feedback and question : comment please⭐
반응형
'Algorithm 💫 > Problem Solving' 카테고리의 다른 글
[백준] 13701번: 중복 제거 (0) | 2021.10.05 |
---|---|
[백준] 11811번: 데스스타 / C++ (0) | 2021.10.05 |
[백준] 3449번: 해밍 거리 (0) | 2021.10.04 |
[백준] 11723번: 집합 (0) | 2021.10.04 |
[백준] 1516번: 게임 개발 / C++ (0) | 2021.10.04 |
댓글