10392 - Factoring Large Numbers
它给了我错误的答案,我不知道为什么!!!
保理大数,您可以假设最多只有一个因子超过1000000. Java代码低于
这些数字将适合gcc的long long int数据类型
我使用大整数类作为long long int
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
while(sc.hasNextBigInteger())
{
BigInteger test = sc.nextBigInteger();
String e ="-1";
if(test.compareTo(new BigInteger(e))==0)
break;
work(test);
}
}
public static void work(BigInteger n)
{
List<BigInteger>list = new ArrayList<BigInteger>();
String s ="2";
for(BigInteger i = new BigInteger(s) ; i.multiply(i).compareTo(n)<=0;i=i.add(BigInteger.ONE))
{
while(n.mod(i).compareTo(BigInteger.ZERO)==0)
{
list.add(i);
n=n.divide(i);
}
}
if(n.compareTo(BigInteger.ONE)>0)
list.add(n);
for(BigInteger j:list)
{
System.out.println("\t"+j);
}
System.out.println();
}
}