【題解】AtCoder ABC161D – Lunlun Number

【題目敘述】https://atcoder.jp/contests/abc161/tasks/abc161_d

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

long long k, ans[100005], idx = 1;
queue <long long> q;

int main(){
    cin >> k;
    for (int i = 1; i < 10; i++){
        q.push(i);
    }
    while (ans[k] == 0){
        long long now = q.front();
        q.pop();
        int n = now % 10;
        if (n != 0) q.push(now * 10 + n - 1);
        q.push(now * 10 + n);
        if (n != 9) q.push(now * 10 + n + 1);
        ans[idx] = now;
        idx++;
    }
    cout << ans[k] << "\n";
}

分享本文 Share with friends