基础练习 分解质因数
问题描述
求出区间[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
4=2*2
5=5
6=2*3
7=7
8=2*2*2
9=3*3
10=2*5
提示
先筛出所有素数,然后再分解。
数据规模和约定
2<=a<=b<=10000
#include<iostream>
#define maxn 10005
#define LL long long
using namespace std;
bool notprime[maxn];
int prime[maxn];
void Era()
{
notprime[1]=true;
prime[0]=0;
for(int i=2;i<=10000;i++)
{
if(!notprime[i])
{
prime[++prime[0]]=i;
for(LL j=(LL)i*i;j<=10000;j+=i)
{
notprime[j]=true;
}
}
}
}
int main()
{
int a,b;
Era();
while(cin >> a >> b)
{
for(int i=a;i<=b;i++)
{
if(!notprime[i])
cout << i << '=' << i;
else
{
int flag=1,tmp=i;
cout << i << '=';
for(int j=1;j<prime[0];j++)
{
if(i%prime[j]==0)
{
int tmp=i;
while(tmp%prime[j]==0)
{
if(flag)
{
flag=0;
cout << prime[j];
}
else
cout << '*' << prime[j];
tmp/=prime[j];
}
}
}
}
cout << endl;
}
}
return 0;
}