问题描述
求出区间[a,b]中所有整数的质因数分解。
输入格式
输入两个整数a,b。
输出格式
每行输出一个数的分解,形如k=a1*a2*a3…(a1<=a2<=a3…,k也是从小到大的)(具体可看样例)
样例输入
3 10
样例输出
3=3
4=2*2
5=5
6=2*3
7=7
8=2*2*2
9=3*3
10=2*5
提示
先筛出所有素数,然后再分解。
数据规模和约定
2<=a<=b<=10000
可以直接用for求解,也可以先筛选素数再分解
直接用for求解
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,i,a1,a2,j;
cin>>a>>b;
for(i=a;i<=b;i++)
{
cout<<i<<"=";
int flag=0;
a1=i;
a2=sqrt(a1);
for(j=2;j<=a2;j++)
{
if(a1%j==0)
{
a1/=j;
if(a1==1)
{
flag=1;
cout<<j<<endl;
}
else
cout<<j<<"*";
j--;
}
}
if(flag==0)
cout<<a1<<endl;
else
continue;
}
return 0;
}