'commit'
This commit is contained in:
25
TangDou/KaoShi/数字分拆.cpp
Normal file
25
TangDou/KaoShi/数字分拆.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user