Advertisement
Guest User

Dumitru

a guest
Dec 9th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. bool distinct(int a, int n){ //functia care verifica daca toate cifrele ale unui numar sunt distincte.
  6. int b[n];
  7. bool d = true;
  8.  
  9. for(int i = 0; i < n; i++){
  10. b[i] = a % 10;
  11. a = a / 10;
  12. }
  13.  
  14. for(int i = 0; i < n; i++)
  15. for(int j = 0; j < n; j++)
  16. if(j != i && b[i] == b[j]) d = false;
  17.  
  18. return d;
  19. }
  20.  
  21. bool sum(int a, int s){ //functia care verifica daca suma cifrelor == s.
  22. int t = 0;
  23. while(a >= 1){
  24. t = t + (a % 10);
  25. a = a / 10;
  26. }
  27. if(t == s) return true;
  28. else return false;
  29. }
  30.  
  31. void qwer(int n, int s, int a){ //functia, care conforma algoritmului greedy, alege elementele necesare si le introduce in multimeea imaginara a solutiilor (afisandu-le)
  32.  
  33. if(a < pow(10, n)){ //in caz ca el va fi mai mare sau egal cu 10^n nu va merge mai departe
  34. if(distinct(a, n) && sum(a, s))
  35. cout << a << endl;
  36. qwer(n, s, a + 1);
  37. }
  38.  
  39. }
  40.  
  41. int main(){
  42.  
  43. int n, s;
  44. cout << "Introduceti n si s. n <= 10, s <= 20" << endl;
  45. cout << "n = ";
  46. cin >> n;
  47. cout << "s = ";
  48. cin >> s;
  49. int v = pow(10, n - 1);
  50. qwer(n, s, v);
  51.  
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement