关于质因数分解,对以下程序不解
#include<iostream>using namespace std;int f(int n){ int nt = n; int cnt = 0; for(int i = 2; i <= nt;i++) if(!(nt%i)){ cout<<i<<" "; cnt++; nt = nt/i; i = 1; } cout<<endl; return cnt;}int main(){ int n; while(cin>>n){ cout<<f(n)<<endl; } return 0;}vector<unsigned int> v;int f(int n){ v.clear(); while(!(n%2)){ v.push_back(2); n /= 2; } for(int x = 3;x <= sqrt(double(n)); x += 2){ while(!(n%x)){ v.push_back(x); n /= x; } } if(n != 1) v.push_back(n); //for(vector<unsigned int>::iterator p = v.begin();p != v.end();++ p) //cout<<*p<<" "; //cout<<endl; return v.size();}
[解决办法]
如果要这样,i*i <=n效率要高很多,sqrt是个复杂得计算