思路:

暴力乱搞。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int n, m, a[10], buf[10];
 8 
 9 bool check()
10 {
11     for (int i = 0; i < n; i++)
12     {
13         buf[i] = a[i];
14     }
15     for (int i = 0; i < n - 1; i++)
16     {
17         for (int j = 0; j < n - i - 1; j++)
18             buf[j] += buf[j + 1];
19     }
20     return buf[0] == m;
21 }
22 int main()
23 {
24     cin >> n >> m;
25     for (int i = 1; i <= n; i++)
26     {
27         a[i - 1] = i;
28     }
29     do
30     {
31         if (check())
32             break;
33     } while (next_permutation(a, a + n));
34     for (int i = 0; i < n; i++)
35     {
36         cout << a[i] << " ";
37     }
38     puts("");
39     return 0;
40 }

 

相关文章: