求解怎么排序

P2404 自然数的拆分问题

式子都出来了但顺序不对QAQ
by 秋名山车神 @ 2017-04-26 17:45:24


我的方法很烦,楼主知道的。。。
by 盐汽水泡面 @ 2017-04-26 17:48:06


你滚,就坐我旁边还下机吧比比
by 秋名山车神 @ 2017-04-26 17:49:56


@[秋名山车神](/space/show?uid=38325) %%%P大神,蒟蒻c++选手只好也只能提供思路了。。。 方法是搜索,开个数组存遍历到的所有自然数,回溯时要记得更新数组,搜到了结果就把整个数组输出。(记得加符号)。
by mrkrnblsa @ 2017-04-28 20:20:57


```cpp #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; int a[10001]= {1},n,total; void print(int t) { for(int i=1; i<=t-1; i++)//输出一种方案解 cout<<a[i]<<'+'; cout<<a[t]<<endl; total++;//方案数加一 } void search(int s,int t) { for(int i=a[t-1]; i<=s; i++) if(i<n) {//当前数 i 要大于等于前 1 位数,且不超过 n a[t]=i;//保存当前拆分数 i s-=i;// s 减去数 i,s 的值继续拆分 if(s==0) print(t);//当 s=0 时,拆分结束输出结果 else search(s,t+1);//当 s>0 时,继续递归 s+=i;//回溯:加上拆分的数,以便产生所有可能的拆分 } } int main() { cin>>n; search(n,1);//将要拆分的数 n 传递给 s return 0; } > ```
by classic @ 2017-08-16 20:05:06


@[classic](/space/show?uid=52151) 抄书有意思吗
by 旗木五五开 @ 2018-10-14 14:30:20


\b 好东西
by JRzyh @ 2020-01-29 10:22:31


@[Zhaoyuhang2008](/user/242524) thanks
by Lhy2009 @ 2020-04-08 18:51:30


|