BASIC-16. 分解质因数
问题描述
求出区间[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
提示
先筛出所有素数,然后再分解。
【我偏不,参考acwing分解质因数更简便:
https://www.acwing.com/problem/content/869/】
数据规模和约定
2<=a<=b<=10000
#include<iostream>
using namespace std;
void divide(int x){
cout<<x<<"=";
bool st=false;
for(int i=2;i<=x/i;i++){
if(x%i==0){
while(x%i==0) {
if(st){
cout<<"*"<<i;
x/=i;
}else{
cout<<i;
st=true;
x/=i;
}
}
}
}
if(x>1) {
if(st) cout<<"*"<<x;
else cout<<x;
}
cout<<endl;
}
int main(){
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++){
divide(i);
}
return 0;
}