POJ-3187 Backward Digit Sums---枚举全排列
题目链接:
https://vjudge.net/problem/POJ-3187
题目大意:
输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)
3 1 2 4 //1~n 全排列中的一个排列
4 3 6
7 9
sum = 16
思路:
直接枚举1-n的全排列,判断之和是不是sum
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 #include<map> 9 #include<cmath> 10 using namespace std; 11 typedef pair<int, int> Pair; 12 typedef long long ll; 13 const int INF = 0x3f3f3f3f; 14 int T, n, m, d; 15 const int maxn = 1e5 + 10; 16 int a[15]; 17 int dir[4][2] = {1,0,0,1,-1,0,0,-1}; 18 int Count(int x[]) 19 { 20 int y[11]; 21 for(int i = 0; i < n; i++)y[i] = x[i]; 22 for(int i = n - 1; i >= 1; i--) 23 { 24 for(int j = 0; j < i; j++) 25 y[j] = y[j] + y[j + 1]; 26 } 27 return y[0]; 28 } 29 int main() 30 { 31 cin >> n >> m; 32 for(int i = 0; i < n; i++)a[i] = i + 1; 33 do 34 { 35 if(Count(a) == m)break; 36 }while(next_permutation(a, a + n)); 37 cout<<a[0]; 38 for(int i = 1; i < n; i++)cout<<" "<<a[i]; 39 cout<<endl; 40 return 0; 41 }
越努力,越幸运