Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- bool distinct(int a, int n){ //functia care verifica daca toate cifrele ale unui numar sunt distincte.
- int b[n];
- bool d = true;
- for(int i = 0; i < n; i++){
- b[i] = a % 10;
- a = a / 10;
- }
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- if(j != i && b[i] == b[j]) d = false;
- return d;
- }
- bool sum(int a, int s){ //functia care verifica daca suma cifrelor == s.
- int t = 0;
- while(a >= 1){
- t = t + (a % 10);
- a = a / 10;
- }
- if(t == s) return true;
- else return false;
- }
- 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)
- if(a < pow(10, n)){ //in caz ca el va fi mai mare sau egal cu 10^n nu va merge mai departe
- if(distinct(a, n) && sum(a, s))
- cout << a << endl;
- qwer(n, s, a + 1);
- }
- }
- int main(){
- int n, s;
- cout << "Introduceti n si s. n <= 10, s <= 20" << endl;
- cout << "n = ";
- cin >> n;
- cout << "s = ";
- cin >> s;
- int v = pow(10, n - 1);
- qwer(n, s, v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement