9.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
提示
先筛出所有素数,然后再分解。
数据规模和约定
2<=a<=b<=10000
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
while(a<2||a>b||a>10000||b<2||b>10000) {//判断输入范围
System.out.println("请重新输入a,b:");
a = sc.nextInt();
b = sc.nextInt();
}
sc.close();//关闭输入口
for(int i=a;i<=b;i++) {
int sushu = 2;
int n = i;
int first = 1;//判断因式个数
while (sushu <= i) {
if (n%sushu != 0) {
sushu++;
} else {
n /= sushu;
if (first == 1) {
System.out.print(i + "=" + sushu);
first++;
} else {
System.out.print("*" + sushu);
}
}
}
System.out.println();
}
}
浙公网安备 33010602011771号