分解质因数(Java)

求出区间[a,b]中所有整数的质因数分解。

输入格式:
输入两个整数a,b。数据规模和约定  2<=a<=b<=10000

输出格式:
每行输出一个数的分解,形如k=a1a2a3…(a1<=a2<=a3…,k也是从小到大的)(具体可看样例)

输入样例:
在这里给出一组输入。例如:
3 10

输出样例:
在这里给出相应的输出。例如:
3=3
4=22
5=5
6=2
3
7=7
8=222
9=33
10=2
5

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int num ;
        int a = sc.nextInt();
        int b = sc.nextInt();
        for (int i = a; i <= b; i++) {
    
    
            System.out.print(i + "=");
            num = i ;
            for (int j = 2; j <= i/2; j++) {
    
    
                while (num % j == 0) {
    
    
                    num = num / j;
                    System.out.print(j);
                    if (num != 1)
                        System.out.print("*");
                }
            }
            if (num==i){
    
    
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51430516/article/details/115102654
今日推荐