Print all multipliers of a given number.
Solution:
vector<vector<int> > printAll(int num) {
vector<vector<int> > solution;
vector<int> answer;
print(num, answer, solution);
}
void print(int &num, vector<int> &answer, vector<vector<int> > &solution) {
if (num < 4) {
answer.push_back(num);
solution.push_back(answer);
answer.pop_back(num);
} else {
int sqrt = floor(sqrt(num));
for (int i = 2; i <= sqrt; i++) {
if ((num % i) == 0 && i <= (num/i)) {
answer.push_back(i);
print(num/i, answer, solution);
answer.pop_back(i);
}
}
}
}
No comments:
Post a Comment