Description
求出区间[a,b]中所有整数的质因数分解。
Input
输入两个整数a,b。
Output
每行输出一个数的分解,形如k=a1a2a3…(a1<=a2<=a3…,k也是从小到大的)(具体可看样例)
Sample Input 1
3 10
Sample Output 1
3=3
4=22
5=5
6=23
7=7
8=222
9=33
10=25
#include <iostream>
using namespace std;
bool Is_prime(int x){
if(x==2||x==3) return true;
else{
for(int i=2;i<x;i++){
if(x%i==0) return false;
}
return true;
}
}
void division(int x){
int i=2;
while(!Is_prime(x)){
if(Is_prime(i)){
if(x%i==0){
cout<<i<<"*";
x=x/i;
}
else
i++;}
else
i++;
}
cout<<x;
}
int main(){
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++){
if(Is_prime(i)==true)
cout<<i<<"="<<i;
else{
cout<<i<<"=";
division(i);
}
cout<<endl;
}
}
这段代码实现了一个程序,输入两个整数a和b,然后输出区间[a, b]内所有整数的质因数分解。每个数的分解形式为k=a1a2a3…,其中k从小到大排列,且a1≤a2≤a3…。例如,当输入310时,输出包括3=3,4=22,5=5等整数的质因数分解。
2565

被折叠的 条评论
为什么被折叠?



