본문 바로가기

백준

1914 하노이탑

오.... 와.....

하노이탑이 2^n - 1 규칙이 있구나. 뭔가 알고리즘 시간에 배운 것 같았는데 몰랐네.

 

그래서 100까지는 2^n - 1으로 계산하는데 20 이하는 그냥 바로 구하는구나. 나는 막...dp를 끼얹나...? 싶었다.

pow사용하는게 정밀도 괜찮은지 걱정되는데 모르겠다. 왜 통과했지??

아 그리고 -1 빼는 것도 이게 5를 안 곱하니 끝자리가 0이 될 수 없어서 이렇게 사용한 것도 신기하다. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>
#include <stdio.h>
#include <math.h>
#include <sstream>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using iis = pair<int, string>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

void hanoi(int n, int from, int middle, int to) {
    if (n == 1) {
        cout << from << " " << to << "\n";
        return;
    }
    hanoi(n - 1, from, to, middle);
    cout << from << " " << to << "\n";
    hanoi(n - 1, middle, from, to);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    int k;
    cin >> k;
    
    string s = to_string(pow(2, k));
    s = s.substr(0, s.find("."));
    
    s[s.size() - 1] -= 1;
    
    cout << s << "\n";
    
    if (k <= 20)
        hanoi(k, 1, 2, 3);
    
    return 0;
}

 

 

 

 

'백준' 카테고리의 다른 글

2644 촌수계산  (0) 2021.12.21
2504 괄호의 값  (0) 2021.12.20
1541 잃어버린 괄호  (0) 2021.12.14
1024 수열의 합  (0) 2021.10.21
1024 수열의 합 [미완]  (0) 2021.10.15