[Baekjoon] 백준 2747번 Python / C

방선생·2025년 1월 19일
0

Baekjoon

목록 보기
10/24

백준 2747번

Python code

import sys
input = sys.stdin.readline

n = int(input())
dp = [0]*46
dp[1] = 1

for i in range(2,n+1):
    dp[i] = dp[i-1] + dp[i-2]


print(dp[n])
  • 알고리즘 공부하면서 푼거라 dp로 풀긴 했는데 그냥 피보나치 생각해서 풀면 됨
    + 입력량도 많지 않아서 sys도 쓸 필요 없음

C code

#include <stdio.h>

int main(void) {
  int n;
  int a=0;
  int b=1;
  int c;
  
  scanf("%d", &n);

  if(n==0){
    printf("0");
  }

  else if(n==1){
    printf("1");
  }

  else{
    for(int i=1;i<n;i++){
      c= a+b;
      a= b;
      b= c;
    }
    
  printf("%d", c);
  }
    
  return 0;
}




브론즈 문제라 해설은 쓰지않고 비슷한 문제 링크 올려드리겠습니다

profile
AI & Robotics

0개의 댓글