Advertisement
edutedu

numere formate din n cifre

Dec 11th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n, c[10], x[100], A, nr=0, nrsol=0;
  5.  
  6. void init(int k)
  7. {
  8. x[k]=0;
  9. }
  10.  
  11. int existasuccesor(int k)
  12. {
  13. return x[k]<n;
  14. }
  15.  
  16. int valid(int k)
  17. {
  18. int nr=0;
  19. if(k==1 && c[x[k]]==0)
  20. return 0;
  21. for(int i=1; i<=k; i++)
  22. nr=nr*10+c[x[i]];
  23. return nr<=A;
  24. }
  25.  
  26. int solutie(int k)
  27. {
  28. return 1;
  29. }
  30.  
  31. void tipar(int k)
  32. {
  33. nrsol++;
  34. int nr=0;
  35. for(int i=1; i<=k; i++)
  36. nr=nr*10+c[x[i]];
  37. cout<<nr<<endl;
  38. }
  39.  
  40. void back1()
  41. {
  42. int k=1;
  43. init(k);
  44. while(k)
  45. {
  46. while(existasuccesor(k))
  47. {
  48. x[k]++;
  49. if(valid(k))
  50. {
  51. tipar(k);
  52. k++;
  53. init(k);
  54. }
  55. }
  56. k--;
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. cout<<"Nr de cifre ";
  63. cin>>n;
  64. cout<<"Introduceti valorile cifrelor: ";
  65. for(int i=1; i<=n; i++)
  66. cin>>c[i];
  67. cout<<"A=";
  68. cin>>A;
  69. back1();
  70. cout<<"\nSunt "<<nrsol<<" solutii";
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement