标签:style blog io color os for sp div log
1 //质因数分解 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 #include<queue> 8 #include<cmath> 9 using namespace std; 10 int top=0,num[10000],time[10000]; 11 void fj(int n) 12 { 13 top=0; 14 memset(num,0,sizeof(num)); 15 memset(time,0,sizeof(time)); 16 for(int i=2;i<=sqrt(n);i++) 17 { 18 if(n%i==0) 19 { 20 top++; 21 n/=i; 22 num[top]=i; 23 time[top]++; 24 while(n%i==0) 25 { 26 n/=i; 27 time[top]++; 28 } 29 } 30 } 31 if(n>1) { 32 top++; 33 num[top]=n; 34 time[top]++; 35 } 36 } 37 int main() 38 { 39 freopen("1.in","r",stdin); 40 freopen("1.out","w",stdout); 41 int n; 42 cin>>n; 43 fj(n); 44 for(int i=1;i<=top;i++) 45 { 46 cout<<num[i]<<" "<<time[i]<<endl; 47 } 48 return 0; 49 }
质因数分解就是找到第一个非1因数连除直到不能整除为止再找下一个。
标签:style blog io color os for sp div log
原文地址:http://www.cnblogs.com/coutendl/p/4077189.html