[TIL/크래프톤 정글] DAY 96

배재준·2025년 6월 13일

크래프톤 정글 - TIL

목록 보기
88/93
post-thumbnail

2025.06.13

TIL(TODAY I LEARN)


  • 오늘한 내용 : 알고리즘 기초 문제

  • WEEK 14 : 실력 다지기 - 알고리즘


  • 숫자의 합

    | 주제: 기초 | 출처: 백준 | 문제번호: 11720 | 난이도: 하 | 문자열 |

    • 풀이
      import sys
      
      input = sys.stdin.readline
      
      n = int(input().strip())
      
      ni = input().strip()
      
      result = 0
      for i in ni:
          result += int(i)
          
      print(result)
  • 8진수, 10진수, 16진수

    | 주제: 기초 | 출처: 백준 | 문제번호: 11816 | 난이도: 하 | |

    • 풀이
      import sys
      
      input = sys.stdin.readline
      
      x = input().strip()
      
      if len(x) > 2 and x[0:2] == '0x':
          y = int(x[2:], 16)
      elif len(x) > 1 and x[0] == '0' and x[1] != 'x':
          y = int(x[1:], 8)
      else:
          y = int(x)
          
      print(y)
  • 히든 넘버

    | 주제: 기초 | 출처: 백준 | 문제번호: 8595 | 난이도: 중 | 문자열 |

    • 풀이
      import sys
      
      input = sys.stdin.readline
      
      n = int(input().strip())
      
      word = input().strip()
      
      result = 0
      
      a = ""
      
      for i in range(n):
          if word[i].isdigit():
              a += word[i]
          else:
              if a != "":
                  result += int(a)
                  a = ""
                  
      # 마지막이 숫자일때 처리
      if a != "":
          result += int(a)        
              
      print(result)
  • Valid Perfect Square

    | 주제: 기초 | 출처: Leetcode | 문제번호: 367 | 난이도: 하 | |

    • 풀이
      
      class Solution:
          def isPerfectSquare(self, num: int) -> bool:
              if num < 2:
                  return True
              
              left, right = 2, num
              
              while left <= right:
                  mid = (left + right) // 2
                  sqr = mid * mid
                  
                  if sqr == num:
                      return True
                  elif sqr < num:
                      left = mid + 1
                  else:
                      right = mid - 1        
              return False
  • Fraction to Recurring Decimal

    | 주제: 기초 | 출처: Leetcode | 문제번호: 166 | 난이도: 중 | |

    • 풀이
      class Solution:
          def fractionToDecimal(self, numerator: int, denominator: int) -> str:
              # 1) 0 처리
              if numerator == 0:
                  return "0"
              # 2) 부호
              sign = "-" if (numerator < 0) ^ (denominator < 0) else ""
              n, d = abs(numerator), abs(denominator)
      
              # 3) 정수부
              integer = n // d
              res = [sign + str(integer)]
      
              # 4) 나머지
              rem = n % d
              if rem == 0:
                  return "".join(res)
      
              # 5) 소수점 준비
              res.append(".")
              seen = {}      # rem → decimals 인덱스
              decimals = []
              idx = 0
      
              # 6) long division 루프
              while rem and rem not in seen:
                  seen[rem] = idx
                  rem *= 10
                  decimals.append(str(rem // d))
                  rem %= d
                  idx += 1
      
              # 7) 순환부 처리
              if rem:  # 순환소수
                  start = seen[rem]
                  non_rep = "".join(decimals[:start])
                  rep     = "".join(decimals[start:])
                  res.append(non_rep + "(" + rep + ")")
              else:
                  res.append("".join(decimals))
      
              return "".join(res)
      

0개의 댓글