求助!!为什么只能输出一行!!

P2404 自然数的拆分问题

@[武怡雯_](/user/472945) 你把当前的和定义为一个参数,m不要,循环条件改成i+sum<=n试试
by zhjzhmh @ 2021-09-06 20:10:21


@[zhjzhmh](/user/233815) 哦哦 wo试试 蟹蟹
by 武怡雯_ @ 2021-09-06 20:12:04


@[zhjzhmh](/user/233815) 还是不行qaq
by 武怡雯_ @ 2021-09-06 20:15:58


@[武怡雯_](/user/472945) 你没回shu(return)
by zhjzhmh @ 2021-09-06 20:18:51


回溯
by zhjzhmh @ 2021-09-06 20:21:51


@[武怡雯_](/user/472945) dfs中`i`应是小于 $m$ 而不是小于等于。
by Universal_xtr @ 2021-09-06 20:21:51


@[zhjzhmh](/user/233815) 加了也不行 www
by 武怡雯_ @ 2021-09-06 20:22:03


@[武怡雯_](/user/472945)
by Universal_xtr @ 2021-09-06 20:22:34


@[Universal_xtr](/user/540148) 改了之后就不输出了 哎
by 武怡雯_ @ 2021-09-06 20:24:18


``` #include<iostream> using namespace std; int n , m , f[10] = {1}; void print(int xx) { for(int i = 1; i < xx; ++i) cout << f[i] <<"+"; cout << f[xx] << endl; } void dfs(int x,int y,int low) { if(y==n) { print(x-1); return; } for(int i = low; i+y <= n; ++i) { f[x] = i; dfs(x + 1,y+i,i); } } int main() { cin >> n; dfs(1,0,1); return 0; } ``` @[武怡雯_](/user/472945) 这样应该可以了
by zhjzhmh @ 2021-09-06 20:25:09


| 下一页