https://www.acmicpc.net/problem/1463
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
// 3으로 나누기
// 2로 나누기
// 1 빼기
//int* dp = new int[n] {0, };
vector<int> dp(n+1, 0);
int now = 1;
dp[1] = 0;
while (now != n) {
int next3 = now * 3;
int next2 = now * 2;
int next1 = now + 1;
if (next3 < n + 1 || next2 < n + 1 || next1 < n + 1) {
}
if (next3 >= n + 1) int pass = 0;
else if (dp[next3] && dp[next3] > dp[now] + 1) dp[next3] = dp[now] + 1;
else if (!dp[next3]) dp[next3] = dp[now] + 1;
if (next2 >= n + 1) int pass = 0;
else if (dp[next2] && dp[next2] > dp[now] + 1) dp[next2] = dp[now] + 1;
else if (!dp[next2]) dp[next2] = dp[now] + 1;
if (next1 >= n + 1) int pass = 0;
else if (dp[next1] && dp[next1] > dp[now] + 1) dp[next1] = dp[now] + 1;
else if (!dp[next1]) dp[next1] = dp[now] + 1;
now++;
}
cout << dp[n];
//delete[] dp;
return 0;
}
}
n을 1로 만드는 문제... 반대로 1을 n으로 만든다고 생각하고 풀었음.
입력받은 n만큼의 크기 배열 만드는 방법: vector arr(n);
조금만 천천히 가주실래요?