TIL | 알고리즘 | 코드카타 11일 (3주 1일)

이도운·2022년 1월 23일
0

TIL

목록 보기
62/73
post-thumbnail

문제

두 개의 input에는 복소수(complex number)가 string 으로 주어집니다. 복소수란 a+bi 의 형태로, 실수와 허수로 이루어진 수입니다.

input으로 받은 두 수를 곱해서 반환해주세요. 반환하는 표현도 복소수 형태의 string 이어야 합니다.

복소수 정의에 의하면 (i^2)는 -1 이므로 (i^2) 일때는 -1로 계산해주세요.

제곱 표현이 안 되어 i의 2제곱을 (i^2)라고 표현했습니다.

풀이

def complex_number_multiply(a, b):
  r1, i1 = a.split('+')
  r2, i2 = b.split('+')
  inum1, _ = i1.split('i')
  inum2, _ = i2.split('i')

  t1 = int(r1) * int(r2)
  t2 = int(r1) * int(inum2)
  t3 = int(inum1) * int(r2)
  t4 = int(inum1) * int(inum2) * -1

  total_r = t1 + t4
  total_inum = t2 + t3

  total_complex = str(total_r) + '+' + str(total_inum) + 'i'

  print(total_r)
  print(total_inum)
  print(total_complex)

  return total_complex

complex_number_multiply("1+1i", "1+1i")
profile
⌨️ 백엔드개발자 (컴퓨터공학과 졸업)

0개의 댓글