问题 1464: [蓝桥杯][基础练习VIP]分解质因数
时间限制: 1Sec 内存限制: 128MB 提交: 1790 解决: 1090
题目描述
求出区间[a,b]中所有整数的质因数分解。
提示
先筛出所有素数,然后再分解。
数据规模和约定
2< =a< =b< =10000
输入
输入两个整数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
理解:
当数n=8时
8%2=0;
4%2=0;
2%2=0;(2/2=1)
此时我们就要输出2×2×2;
n=6时
6%2=0;
3%2=1(j++);
3%3=0;(3/3=1)
此时输出2×3
#include<bits/stdc++.h>
using namespace std;
int a[10005];
int t=0;
void susu(int n)
{
int flag;
for(int i=2;i<=n;i++)
{
flag=1;
for(int j=2;j<i;j++)
{
if(i%j==0){flag=0;break;}
}
if(flag==1){a[t]=i;t++;}
}
}
int main()
{
int n,m,flag,temp,temp2,temp3;
susu(10000);
cin>>n>>m;
for(int i=n;i<=m;i++)
{
flag=0;
for(int j=0;j<=m;j++)
{
if(i==a[j]){cout<<i<<"="<<a[j]<<endl;flag=1;break;}
}
if(flag==1)continue;
cout<<i<<"=";
temp=i;
for(int j=0;j<=m;j++)
{
if(temp%a[j]>=1)continue;
temp2=temp/a[j];
temp3=a[j];
if(temp2==1){cout<<a[j];break;}
if(temp%temp3==0){cout<<a[j]<<"*";temp/=temp3;j--;}
}
cout<<endl;
}
return 0;
}