๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1463๋ฒˆ - 1๋กœ ๋งŒ๋“ค๊ธฐ

by dkswnkk 2021. 11. 11.

๋ฌธ์ œ

 

1463๋ฒˆ: 1๋กœ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 106๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <vector>
using namespace std;

vector<int>dp(1e7);
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int N; cin >> N;
    for (int i = 2; i <= N; i++) {
        dp[i] = dp[i - 1] + 1;    //ํ˜„์žฌ์˜ ์ˆ˜์—์„œ 1์„ ๋นผ๋Š” ๊ฒฝ์šฐ
        if(i% 2 ==0 ) dp[i] = min(dp[i], dp[i / 2] + 1);    //ํ˜„์žฌ์˜ ์ˆ˜๊ฐ€ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ
        if (i % 3 == 0) dp[i] = min(dp[i], dp[i / 3] + 1);    //ํ˜„์žฌ์˜ ์ˆ˜๊ฐ€ 3์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ
    }
    cout << dp[N];
}

๋Œ“๊ธ€