Files
python/TangDou/KaoShi/数字分拆.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

25 lines
752 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
/*
输入8
答案3
*/
int n;
int res;
// r:待分拆的数字
// last:记录上一个分拆的数字是什么
void dfs(int u, int r, int last) {
if (u == 4) {
if (r == 0) res++; //如果完成分拆的数字恰好等于n,说明是成功的分拆,否则就是没用完有剩余
return;
}
for (int i = last + 1; i <= r; i++) //必须大于上一个,才能保证唯一不重复
if (i != 3 && i != 7) dfs(u + 1, r - i, i); //当前位置不能分配3和7
}
int main() {
cin >> n;
dfs(1, n, 0); //站在第1个箱子面前待分拆的数是n,上一个分拆完的数字是0
printf("%d\n", res);
return 0;
}