티스토리 뷰


title: "2×n 타일링(11726)"
category: 백준[Class-3]
tags: [C++, JavaScript, 백준]
date: "2021-04-17"


문제 링크

2×n 타일링(11726)

C++

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// 문제 풀이 함수
void solution(){
    int n;
    cin >> n;

    if(n==1) cout<<1<<"\n";
    else{
        // 2x1: 1
        // 2x2: 2
        // 2x3: 3
        // 2x4: 5
        // 2x9: 55
        vector<int> dp(n+1, 0);
        dp[1]=1;
        dp[2]=2;
        for(int i=3; i<=n; i++){
            dp[i] = dp[i-1]+dp[i-2];
            dp[i] %= 10007;
        }
        cout<<dp[n]<<"\n";
    }
}

bool exists(const char* fileName){
    FILE* fp;
    if((fp = fopen(fileName, "r"))){
        fclose(fp);
        return true;
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    if(exists("stdin")){
        freopen("stdin", "r", stdin);
        solution();
        fclose(stdin);
    }
    else{
        solution();
    }

    return 0;
}

JavsScript

const fs = require("fs");
const input = fs.readFileSync("dev/stdin").toString().trim().split("\n");

// 문제 풀이
const n = +input[0];

if (n === 1) {
  console.log(1);
} else {
  // 2x1: 1
  // 2x2: 2
  // 2x3: 3
  // 2x4: 5
  // 2x9: 55
  const dp = Array.from({ length: n + 1 }, () => 0);
  dp[1] = 1;
  dp[2] = 2;
  for (let i = 3; i <= n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2];
    dp[i] %= 10007;
  }

  // print
  console.log(dp[n]);
}
728x90
반응형

'Baekjoon Solutions > Class-3' 카테고리의 다른 글

[백준] 파도반수열(9461)  (0) 2021.04.20
[백준] 2xn 타일링 2(11727)  (0) 2021.04.17
[백준] 구간 합 구하기 4(11659)  (0) 2021.04.14
[백준] ATM(11399)  (0) 2021.04.12
[백준] 패션왕 신해빈(9375)  (0) 2021.04.09
댓글
05-15 01:00
링크