백준 12813

HJ seo·2022년 9월 23일
0

Coding Test(Python)

목록 보기
31/45

문제 링크

문제 풀이 개선. 이전 코드와 다르게 bitmasking 본연의 코드를 사용했고, 이 경우가 훨씬 빠른 결과가 나온다. from here.

수정 코드.

A = int(input(), 2)
B = int(input(), 2)

print(bin(2**100000 + (A & B))[3:])
print(bin(2**100000 + (A | B))[3:])
print(bin(2**100000 + (A ^ B))[3:])
print(bin(2**100001 + ~A)[3:])
print(bin(2**100001 + ~B)[3:])

이전 코드.(here에 있는 코드)

from sys import stdin
a = stdin.readline().strip()
b = stdin.readline().strip()

op_and = ''
op_or = ''
op_xor = ''
maxi = min(a.index('1'),b.index('1'))
not_a = ''
not_b = ''
for i in range(100000):
    op_and += '1' if a[i] == b[i] and a[i] == '1' else '0'
    op_or += '1' if a[i] == '1' or b[i] == '1' else '0'
    not_a += '0' if a[i] == '1' else '1'
    not_b += '0' if b[i] == '1' else '1'
    
    if maxi>i:
        op_xor += '0'
    elif a[i] != b[i]:
        op_xor += '1'
    else:
        op_xor += '0'
    
print(op_and)
print(op_or)
print(op_xor)
print(not_a)
print(not_b)
profile
다양한 분야에 관심이 많은 초보 개발자 입니다.

0개의 댓글