求一个整数的所有因子
2017-03-26 本文已影响118人
贰拾贰画生
int num;
cin>>num;
vector<int> factors;
factors.push_back(1);
if(num == 1) return factors;
factors.push_back(num);
int right = num / 2;
for (int i = 2; i < right; ++i) {
if (num % i == 0) {
right = num / i;
factors.push_back(i);
if(i != right) factors.push_back(right);
}
}
return factors;