非常神奇的输出,玄关,急

P2404 自然数的拆分问题

@[lucy2012](/user/1252442) 你这输出是什么啊?加号没有,换行没有?
by cui_cui_sha @ 2024-04-20 16:38:36


sum每次都是0啊,有什么意义?
by cui_cui_sha @ 2024-04-20 16:40:59


```cpp #include <bits/stdc++.h> using namespace std; int n,ans[50],cnt = 0; void dfs(int sum){ if (sum == n) { if (cnt == 1) return; for (int i = 1; i <= cnt; i++) { printf("%d",ans[i]); if (i != cnt) printf("+"); } printf("\n"); return; } for (int i = ans[cnt]; i <= (n - sum) / 2; i++) { if (i == 0) continue; cnt++; ans[cnt] = i; dfs(sum + i); ans[cnt] = 0; cnt--; } cnt++; ans[cnt] = n - sum; dfs(n); ans[cnt] = 0; cnt--; return; } int main(){ scanf("%d",&n); dfs(0); return 0; } ```
by liuruiqing @ 2024-04-20 16:42:41


@[lucy2012](/user/1252442) 我还是不太理解你的代码
by liuruiqing @ 2024-04-20 16:43:17


``` #include<bits/stdc++.h> using namespace std; long long n, num[111]; void dfs(int i, int x) { if (x == 0) { for (int j = 1; j < i; j++) { if (j > 1) cout << '+'; cout << num[j]; } cout << endl; } for (int v = num[i - 1]; v <= x && v < n; v++) { num[i] = v; dfs(i + 1, x - v); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; num[0] = 1; dfs(1, n); return 0; } ```
by cui_cui_sha @ 2024-04-20 16:43:52


@[lucy2012](/user/1252442) 我也不太理解你的代码
by cui_cui_sha @ 2024-04-20 16:44:17


|