poj 3187 暴力枚举
没啥好讲的这道题,主要是赛场做题的心态。应该是很燥吧,以至于题目也看错。如果没看错题,我是否就有勇气来个暴力呢? 不管怎样,如果有思路就试试吧,,,,代码如下
#include <iostream> #include <cstdio> using namespace std; int n, sum; int ans = 0; void getsum(const int *A) { int B[12]; for(int i = 0; i < n; i++) B[i] = A[i]; for(int i = n - 1; i >= 0; i--) { for(int j = 0; j < i; j++) B[j] = B[j] + B[j+1]; } if(B[0] == sum) { ans = 1; int fir = 1; for(int i = 0; i < n;i++) { if(fir) fir = 0; else printf(" "); cout << A[i]; } cout<<endl; } return; } void dfs(int *A, int cur) { if(ans) return; if(cur == n) { getsum(A); return; } for(int i = 1; i <= n; i++) { int ok = 1; for(int j = 0; j < cur; j++) if(A[j] == i)ok = 0; if(ok) { A[cur] = i; dfs(A, cur + 1); } } return; } int main() { cin >> n >> sum; int A[12]; dfs(A, 0); }
print “ 欢迎来到渣小狼的博客,这既是博客,也是日记,里面记录了小狼的学习经历还有一些小狼的见解,非常希望每一个来到这里的人能够留下只言片语,更加的希望留下的是对于小狼的不足的补充,谢谢(*^__^*) 嘻嘻……”
浙公网安备 33010602011771号