Advertisement
Dani_info

partitii multime

Dec 12th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. int x[100], n, nrsol=0, Max;
  8.  
  9. void init(int k){
  10.     x[k]=0;
  11. }
  12.  
  13. int existasuccesor(int k){
  14.     Max=0;
  15.     for(int i=1;i<k;i++)
  16.         if(x[i]>Max)
  17.             Max=x[i];
  18.     return x[k]<=Max;
  19. }
  20.  
  21. int estevalid(int k){
  22.     return true;
  23. }
  24.  
  25. int solutie(int k){
  26.     return k==n;
  27. }
  28.  
  29. void tipar(){
  30.     nrsol++;
  31.     cout<<"\nParitia "<<nrsol<<":"<<endl;
  32.     if (Max<x[n]) Max=x[n];
  33.     for(int m=1;m<=Max;m++)
  34.     {
  35.        cout<<"Submultimea "<<m<<":  ";
  36.        for(int i=1;i<=n;i++)
  37.             if(x[i]==m) cout<<i<<" ";
  38.        cout<<endl;
  39.     }
  40. }
  41.  
  42. void back(){
  43. int k=1;
  44. init(1);
  45. while(k>0)
  46. {
  47.     while (existasuccesor(k))
  48.     {
  49.         x[k]=x[k]+1;
  50.         if (estevalid(k))
  51.             if (solutie(k)) tipar();
  52.             else
  53.                 {
  54.                         k++;
  55.                         init(k);
  56.                 }
  57.     }
  58.   k--;
  59. }
  60. }
  61.  
  62. int main(){
  63. cout<<"cate elemente sunt? "; cin>>n;
  64. back();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement