题解 | #字符串分隔#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
C++ 3ms
根号一下排除大半
2单独计算以便后面降低循环次数
从3开始每次加2,减少一半循环
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
long int num;
cin>>num;
long sq=sqrt(num);
vector<int> v;
while(num%2==0)
{
v.push_back(2);
num/=2;
}
for(int i=3;i<=sq;i+=2)
{
while(num%i==0)
{
v.push_back(i);
num/=i;
}
}
if(num!=1) // 如果上述运行成功代表num被整除必等于1;否则本身就是个质数
v.push_back(num);
for(auto i:v) cout<<i<<" ";
return 0;
}
#include<vector>
#include<cmath>
using namespace std;
int main()
{
long int num;
cin>>num;
long sq=sqrt(num);
vector<int> v;
while(num%2==0)
{
v.push_back(2);
num/=2;
}
for(int i=3;i<=sq;i+=2)
{
while(num%i==0)
{
v.push_back(i);
num/=i;
}
}
if(num!=1) // 如果上述运行成功代表num被整除必等于1;否则本身就是个质数
v.push_back(num);
for(auto i:v) cout<<i<<" ";
return 0;
}