LanQiaoTestCodes/求解质因子.java

45 lines
861 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package LanQiaoOJ;
import java.util.Scanner;
/*
* 给定一个数字N请你求出它的所有质因子。
输入描述
输入仅一行包含为一个整数N。
1≤W≤10^12。*/
public class 求解质因子 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long n=sc.nextLong();
for(long i=2;i<n/i;i++) {
//判断是否为质因子
if(n%i==0) {
System.out.print(i+" ");
//将n除尽
while(n%i==0)n/=i;
}
}
if(n>1)
System.out.print(n);
}
}
/*此方法蓝桥OJ上无法通过超时——暴力破解
* public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long n=sc.nextLong();
for(long i=2;i<n;i++) {
if(n%i==0&&check(i)) {
System.out.print(i+" ");
}
}
}
static Boolean check(long x) {
for(long i=2;i<=x/i;i++) {
if(x%i==0)
return false;
}
return true;
}*/