0
点赞
收藏
分享

微信扫一扫

HJ06 J6 质数因子


​​J6 质数因子​​

#include <iostream>
#include <list>

class HJ06 {
private:
double static sqrt(int num, double err = 1e-15) {
if (num < 0) {return -1;}
auto c = (double)num;
double t = c;
while (std::abs(t-c/t) > err * t) {
t = (c/t + t) / 2.0;
}
return t;
}
public:
static std::list<int> primeFactors(int num) {
std::list<int> ans;
int max;

while (num % 2 == 0) {
num /= 2;
ans.push_back(2);
}
while (num % 3 == 0) {
num /= 3;
ans.push_back(3);
}
max = int(sqrt(num, 1e-2) + 0.5);
// for (int i = 5; i*i <= num; i++) {
for (int i = 5; i <= max; i++) {
while (num % i == 0) {
num /= i;
ans.push_back(i);
}
}
if (num != 1) {
ans.push_back(num);
}
return ans;
}
};

int main() {
// int n = 180;
// int n = 2000000014;
int n = 0;
std::cin >> n;

std::list<int> factors = HJ06::primeFactors(n);
auto it = factors.begin(); // std::list<int>::const_iterator
std::cout << *it++;
for (; it != factors.end(); ++it) {
std::cout << " " << *it;
}
return 0;
}

float 四舍五入取近似值可以 +0.5 再转int

​​HJ7 取近似值​​

举报

相关推荐

0 条评论