- 论坛徽章:
- 0
|
本帖最后由 didslr 于 2013-09-14 13:41 编辑
试试这个- #include <stdio.h>
- #define N 100
- int n, m, a[N], b[N];
- void Perm(int a[], int a_len, int a_pos, int b[], int b_len, int b_pos)
- {
- int i, ai, sum;
- if(b_pos >= b_len)
- {
- for(sum = i = 0; i < b_len; sum += b[i++]) ;
- if(sum == m && b[b_len - 1] != 0)
- {
- for(i = 0; i < b_len; i++) printf("%d ", b[i]);
- puts("");
- }
- }
- else for(ai = a_pos; ai < a_len; ai++)
- {
- b[b_pos] = a[ai];
- Perm(a, a_len, ai + 1, b, b_len, b_pos + 1);
- }
- }
- int main()
- {
- int i;
- scanf("%d%d", &n, &m);
- for(i = 0; i < n; i++) a[i] = i + 1;
- for(i = 1; i <= n; Perm(a, n, 0, b, i++, 0)) ;
- return 0;
- }
复制代码 |
|