Suma de bani. (bancnotele trebuie sa fie in ordine crescatoare)
#include<iostream>Sa se afiseze toate sirurile crescatoare care incep cu n si se termina cu n+k.
using namespace std;
int b[100],x[100],nr=0,n,f[100]={0},minim=INT_MAX,mi;
void afisare(int n)
{int i;
for(i=1;i<=n;i++)
if(x[i])
cout<<x[i]<<"*"<<b[i]<<" ";
cout<<endl;
nr++;}
int suma(int x[], int n)
{int i,s=0;
for(i=1;i<=n;i++)
s=s+x[i];
return s;
}
void back(int i,int s,int nrb)
{int j;
if(i==n)
{if(s%b[i]==0)
{x[i]=s/b[i];
afisare(i);
if(nrb+j<minim)
{minim=nrb+j;mi=i;
for(int k=1;k<=i;k++)
f[k]=x[k];}}
}
else{
for(j=0;j<=s/b[i];j++)
{x[i]=j;
if(s==j*b[i])
{afisare(i);
if(nrb+j<minim)
{minim=nrb+j;mi=i;
for(int k=1;k<=i;k++)
f[k]=x[k];}}
else back(i+1,s-j*b[i],nrb+j);
}
}
}
int main()
{int i,j,s,nrb=0;
cin>>s>>n;
for(i=1;i<=n;i++)
cin>>b[i];
back(1,s,0);
cout<<endl<<nr<<" solutii"<<endl;
for(j=1;j<=mi;j++)
if(f[j])
cout<<f[j]<<"*"<<b[j]<<" ";
}
#include<iostream>Sa se afiseze toate submultimile unei multimi care au suma elementelor egala cu o suma data.
using namespace std;
int x[100],n,k,nr=0;
int afisare(int n)
{int i;
for(i=1;i<=n;i++)
cout<<x[i]<<" ";
cout<<endl;
nr++;}
void back(int i)
{int j;
for(j=x[i-1]+1;j<=n+k;j++)
{x[i]=j;
if(j==n+k)
afisare(i);
else back(i+1);
}
}
int main()
{cin>>n>>k;
x[1]=n;
back(2);
}
#include <iostream>
#include <ctime>
using namespace std;
int x[100],n,s,a[100];
void afisare(int n)
{int i;
for(i=1;i<=n;i++)
cout<<a[x[i]]<<"+";
cout<<"\b ";
cout<<endl;
}
void back(int i, int s)
{int j,sr;
for(j=x[i-1]+1;j<=n;j++)
{x[i]=j;
sr=s-a[x[i]];
if(sr==0)
afisare(i);
else if(sr)
back(i+1,sr);
}
}
int main()
{int i;
cout<<"n=";cin>>n;
cout<<"s=";cin>>s;
for(i=1;i<=n;i++)
cin>>a[i];
back(1,s);
}