#include <stdio.h> #include <stdlib.h> int Sum(int *buff, int len) { int sum = 0; for (int i = 0; i < len; i++) { sum += buff[i]; } return sum; } void Print(int *buff, int len) { for (int i = 0; i < len; i++) { printf("%d ", buff[i]); } // printf("\n"); } int Perm(int *buff, int *len, int start, int m, int *count) { int sum = Sum(buff, *len); int min = ((m - sum) < start) ? (m - sum) : start; for (int i = min; i > 0; i--) { if ((sum + i) == m) { (*count)++; Print(buff, *len); printf("%d\n", i); continue; } buff[*len] = i; (*len)++; Perm(buff, len, i, m, count); (*len)--; } } int main() { int buff[10] = {0}; int len = 0; int count = 0; int n; scanf("%d", &n); Perm(buff, &len, n, n, &count); printf("n = %d, count = %d\n", n, count); return 0; }
原文地址:https://www.cnblogs.com/felixr/p/11407945.html
时间: 2024-11-02 03:22:47