ZOJ-2576(dfs)

来源:互联网 发布:授权回调域名校验出错 编辑:程序博客网 时间:2024/06/11 23:55

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1576

分析:给出N,打印出2N的所有分割方式,使得后一个数大于等于前一个数,暴搜即可


#include <cstdio>int n, m, st[22], top;void out(){printf("{%d", st[0]);for(int i = 1; i < top; ++i) printf(",%d", st[i]);puts("}");}void dfs(int pre, int res){st[top++] = pre;if(res >= pre){for(int i = pre; i <= res && i <= n; ++i){dfs(i, res - i);}}else if(!res) out();--top;}int main(){while(scanf("%d", &n), n){m = n << 1;for(int i = 1; i <= n; ++i){top = 0;dfs(i, m - i);}puts("");}return 0;}


0 0