Basic exercises to decompose prime factors--(Blue Bridge exercises)

Find the prime factorization of all integers in the interval [a, b].

Input format  
Input two integers a, b.
Output format  
Each line outputs a decomposition of a number, in the form k=a1 a2 a3…(a1<=a2<=a3…, k is also from small to large) (see the example for details)
sample input
3 10
sample output
3=3
4=2 2
5=5
6=2
3
7=7
8=2 2 2
9=3 3
10=2
5

#include <iostream>
#include <vector>
using namespace std;
vector <int> prime[10001];
void into()
{
    
    
 int i,j,temp;
 for(i=2;i<=10001;i++)
 {
    
    
  if(prime[i].size()==0)
  {
    
    
   for(j=i;j<=10001;j=j+i)
   {
    
    
    temp=j;
    while(temp%i==0)
    {
    
    
     prime[j].push_back(i);
     temp=temp/i;
    }
   }
  }
 }
 return;
}
int main()
{
    
    
 int i,a,b;
 into();
 vector <int>::iterator it;
 cin>>a>>b;
 for(i=a;i<=b;i++)
 {
    
    
  cout<<i<<"=";
  for(it=prime[i].begin();it!=prime[i].end();it++)
  {
    
    
   if(it==prime[i].end()-1)
   cout<<*it<<endl;
   else {
    
    
    cout<<*it<<"*";
   }
  }
  if(prime[i].size()==0)
  cout<<i<<endl;
 }
 return 0;
}

Find any prime factor

#include <iostream>
using namespace std;
int main()
{
    
     
int n,n0,i=2; 
cout<<"请输入一个数"<<endl; 
cin>>n; 
n0=n; 
while(n0>=i) 
{
    
       
if(n0%i==0)   
{
    
        
cout <<i <<" "; 
while(n0%i==0)    
n0 /= i;   
}    
i++; 
} 
cout <<endl; 
system("pause");
} 

Guess you like

Origin blog.csdn.net/HT24k/article/details/107101205