Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int Sum(int *tab, int n);
  5. void PrintTable(int *tab, int n);
  6.  
  7. int main()
  8. {
  9.     int k, p, *t, s, incIdx, i, diff, count = 0;
  10.    
  11.     printf("Podaj liczbe kul > ");
  12.     scanf("%d", &k);
  13.     fflush(stdin);
  14.     printf("Podaj liczbe pojemnikow > ");
  15.     scanf("%d", &p);
  16.     fflush(stdin);
  17.    
  18.     t = (int*) malloc(p * sizeof(int));
  19.    
  20.     incIdx = p - 2;
  21.     for (i = 0; i < p - 1; i++)
  22.         t[i] = i;
  23.    
  24.     while(1)
  25.     {
  26.         diff = k - sum(t, p - 1);
  27.        
  28.         if (diff > t[p - 2])
  29.         {
  30.             incIdx = p - 2;
  31.             t[p - 1] = diff;
  32.             PrintTable(t, p);
  33.             count++;
  34.         }
  35.         else
  36.         {
  37.             incIdx--;
  38.             if (incIdx == -1)
  39.                 break;
  40.         }
  41.        
  42.         t[incIdx]++;
  43.        
  44.         for (i = incIdx + 1; i < p - 1; i++)
  45.             t[i] = t[i - 1] + 1;
  46.     }
  47.    
  48.     printf("\nIlosc kombinacji: %d\n", count);
  49.    
  50.     free(t);       
  51.     return 0;
  52. }
  53.  
  54. int sum(int *tab, int n)
  55. {
  56.     int sum = 0, i;
  57.     for (i = 0; i < n; i++)
  58.         sum += tab[i];
  59.            
  60.     return sum;
  61. }
  62.  
  63. void PrintTable(int *tab, int n)
  64. {
  65.     int i;
  66.    
  67.     for (i = 0; i < n; i++)
  68.         printf("|%d|, ", tab[i]);
  69.    
  70.     putchar('\n');
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement