#include using namespace std; typedef long long LL; struct Node { int number; // 数字 int cnt; // 几根木棍 } a[6] = {{1, 2}, {2, 5}, {4, 4}, {6, 6}, {7, 3}, {8, 7}}; /** * u 第几个位置 * r 剩余木棍数 * s 当前数字 */ LL res = LONG_LONG_MAX; void dfs(int u, int r, LL s) { if (r == 0) { res = min(res, s); return; } for (int i = 0; i < 6; i++) if (a[i].cnt <= r) { if (u > 1 && a[i].number == 6) dfs(u + 1, r - a[i].cnt, s * 10); else dfs(u + 1, r - a[i].cnt, s * 10 + a[i].number); } } int main() { // 我们要找到一个数字,这个数字要使用掉指定数量的木棍,比如 n=1 // n=1 拼不出来,输出-1 // n=2 1 // n=3 7 // n=4 4 // n=5 2 // n=6 6 // n=7 8 // n=8 10 // n=9 18 // n=10 22 // n=11 20 // n=12 28 for (int i = 1; i <= 40; i++) { res = LONG_LONG_MAX; if (i > 1) dfs(1, i, 0); else res = -1; cout << "n=" << i << " " << res << endl; } return 0; }