跳到正文

剪贴板

剪贴板 y5r3uytm

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <iostream>
#include <vector>
#include <string>
#include <string.h>

const int pow_10[] = {1, 10, 100, 1000, 10000, 100000, 1000000};

int cur[1000000];
std::vector<int> edge[1000000], path;
std::string ans[7];

void Dfs(int u)
{
    for (int &i = cur[u]; i < edge[u].size();)
    {
        int v = edge[u][i++];
        Dfs(v);
    }

    path.push_back(u);
}

void Init()
{
    ans[1] = "0123456789";

    for (int i = 2; i <= 6; i++)
    {
        memset(cur, 0, sizeof(cur));
        for (int j = 0; j < pow_10[i]; j++) edge[j].clear();
        path.clear();

        for (int j = 0; j < pow_10[i]; j++)
        {
            edge[j / 10].push_back(j % pow_10[i - 1]);
        }

        Dfs(0);
        for (int j = 2; j < i; j++) ans[i] += '0';

        for (int j = path.size() - 1; j >= 0; j--)
        {
            ans[i] += '0' + path[j] % 10;
        }
    }
}

int main(int argc, char const *argv[])
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    Init();

    while (true)
    {
        int n;
        std::cin >> n;
        if (!n) break;
        std::cout << ans[n] << '\n';
    }

    return 0;
}