Advertisement
pdaogu

SERIAL_UPGRADE

Oct 9th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #define maxn 100
  3. int N, cnt = 0;
  4. int sum[maxn];
  5. int a[maxn];
  6.  
  7. void output (int n) {
  8.     for (int i = 1; i <= n; ++i) {
  9.         printf("%d", a[i]);
  10.         if (i < n) {
  11.             printf(" + ");
  12.         }
  13.     }
  14.     printf("\n");
  15.     ++cnt;
  16. }
  17.  
  18. bool check (int i, int v) {
  19.     return true;
  20. }
  21.  
  22. void Attempt (int k, int v0) {
  23.     for (int v = v0; v <= N-sum[k-1]; ++v) {
  24.         if (check(k, v)) {
  25.             a[k] = v;
  26.             sum[k] = sum[k-1] + a[k];
  27.             if (sum[k] == N) {
  28.                 output(k);
  29.             } else {
  30.                 Attempt(k+1, v);
  31.             }
  32.             a[k] = 0;
  33.             sum[k] = 0;
  34.         }
  35.     }
  36. }
  37.  
  38. int main () {
  39.     scanf("%d", &N);
  40.     Attempt(1, 1);
  41.     printf("\nCount: %d\n", cnt);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement