Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #define NR 11
- using namespace std;
- int v[NR],x[NR],n,c,k;
- void init(int k)
- {
- x[k]=0;
- }
- bool succesor(int k)
- {
- if(x[k]<n)
- return true;
- else
- return false;
- }
- bool continuare(int k)
- {
- for(int i=1;i<=k-1;++i)
- if(x[k]==x[i])
- return false;
- return true;
- }
- bool solutie(int k)
- {
- if(k==c+1)
- return true;
- else
- return false;
- }
- void afisare()
- {
- if(v[x[1]]==0)
- return;
- for(int i=1;i<=c;++i)
- cout<<v[x[i]];
- cout<<'\n';
- }
- void backtracking()
- {
- int k;
- k=1;
- init(1);
- while(k!=0)
- if (solutie(k))
- {
- afisare();
- --k;
- }
- else
- if(succesor(k))
- {
- ++x[k];
- if(continuare(k))
- ++k;
- }
- else
- {
- init(k);
- --k;
- }
- }
- int main()
- {
- cin>>n>>c;
- int i=0;
- while(n)
- v[++i]=n%10,n/=10;
- n=i;
- sort(v,v+n+1);
- backtracking();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement