Skip to content

HJ6 质数因子 #6

@sumulige

Description

@sumulige
  • 功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )
  • 数据范围: $1 \le n \le 2 \times 10^{9} + 14 $
  • 取模遍历+问题分解+分类讨论
    • 注意取值范围 nextint nextlong
    • 取模判断 整除就返回此因子 然后继续递归
    • 记得相加
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int input = in.nextInt();

        int factor = 2;

        while(input > 1)
            if(input % factor == 0){
                System.out.print(factor + " ");
                input /= factor ;
            }else{
                factor ++;

            }
        }



public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    System.in));

        String input = "";

        StringBuilder stringBuilder = new StringBuilder();

        while ((input = bufferedReader.readLine()) != null) {
            int parseInt = Integer.parseInt(input);
            for (int i = 2; i <= Math.sqrt(parseInt); i++) {
                if ( parseInt % i == 0) {
                    stringBuilder.append(i).append(" ");
                    parseInt /= i;
                    i --;
                }
            }
            stringBuilder.append(parseInt).append(" ");
            System.out.println(stringBuilder.toString());
        }

    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions