Advertisement
Guest User

Țava =))))

a guest
Oct 16th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ofstream g("bara.out");
  7.  
  8. unsigned int L,n,suma=0;
  9. unsigned int bara[100],st[100];
  10.  
  11. void init(int k)
  12. {
  13.     if(k==1)
  14.         st[k]=0;
  15.     else
  16.         st[k]=st[k-1]-1;
  17. }
  18. int surjectiva(int k)
  19. {
  20.     bool ok;
  21.     for(int i=1;i<=n;i++)
  22.     {
  23.         ok = false;
  24.         for(int j=1;j<=k;j++)
  25.             if(bara[i] == bara[st[j]])
  26.                 ok = true;
  27.         if(!ok)
  28.             return 0;
  29.     }
  30.     return 1;
  31. }
  32. int succesor(int k)
  33. {
  34.     if(st[k] < n )
  35.     {
  36.         st[k] ++;
  37.         return 1;
  38.     }
  39.     else
  40.     {
  41.         suma -= bara[st[k-1]];
  42.         return 0;
  43.     }
  44. }
  45. int valid(int k)
  46. {
  47.     if(bara[st[k]] + suma <= L)
  48.     {
  49.         suma +=bara[st[k]];
  50.         return 1;
  51.     }
  52.     return 0;
  53. }
  54. int solutie(int k)
  55. {
  56.     return suma == L;
  57. }
  58. void tipar(int k)
  59. {
  60.     int este = 1;
  61.     g << "#";
  62.     for(int i = 1; i < k; i++)
  63.     {
  64.         cout<<bara[st[i]]<<" ";
  65.         for(int j = 1; j < bara[st[i]]; j++)
  66.             g << "-";
  67.         g << "|";
  68.     }
  69.  
  70.     for(int i = 1; i<bara[st[k]]; i++)
  71.         g << "-";
  72.     g << "#";
  73.     g << "\n\n\n";
  74.     suma -= bara[st[k]];
  75.  
  76.     cout<<bara[st[k]];
  77.     cout<<'\n';
  78. }
  79. void bkt(int k)
  80. {
  81.     init(k);
  82.     while(succesor(k))
  83.         if(valid(k))
  84.             if(solutie(k) && surjectiva(k))
  85.                 tipar(k);
  86.             else
  87.                 bkt(k+1);
  88. }
  89. void Citire(){
  90.     ifstream f("bara.in");
  91.     f>>L;f>>n;
  92.     for(int i=1;i<=n;i++)
  93.     {
  94.         f>>bara[i];
  95.     }
  96.     bkt(1);
  97.     f.close();
  98. }
  99. int main()
  100. {
  101.     Citire();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement