美文网首页蓝桥杯题目
[蓝桥杯]计算质因子

[蓝桥杯]计算质因子

作者: 二十五六岁的你 | 来源:发表于2020-01-30 18:06 被阅读0次

    题目描述

    输入一个整数,输出其所有质因子。

    数据规模和约定
    1< =n< =10000。

    输入

    输入只有一行,包含一个整数n。

    输出

    输出一行,包含若干个整数,为n的所有质因子,按照从小到大的顺序排列。

    样例输入

    6 
    

    样例输出

    2 3
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * User: 76147
     * Date: 2020-01-26
     * Time: 18:17
     * Description:
     */
    public class 计算质因子 {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                int n = sc.nextInt();
                for (int i = 2; i < n; i++) {
                    int flag = 0;
                    for (int j = 2; j <= Math.sqrt(i); j++) {
                        if (i % j == 0)
                            flag++;
                    }
                    if (flag == 0 && n % i == 0)
                        System.out.print(i + " ");
                }
            }
        }
    
    }   
    

    相关文章

      网友评论

        本文标题:[蓝桥杯]计算质因子

        本文链接:https://www.haomeiwen.com/subject/mhtwthtx.html