【发布时间】:2016-10-01 13:29:49
【问题描述】:
public class LargestPrimeFactor {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
int num = 0, temp = 0;
while(cases!=0) {
num = scan.nextInt();
for(int i=2; i<=num; i++)
if(num%i==0) {
if(isPrime(i)) {
if(temp<i)
temp = i; //to always have the largest factor
}
num/=i; // to reduce the iterations for a large number.
}
System.out.println(temp);
cases--;
}
}
public static boolean isPrime(int num) {
if ( num > 2 && num%2 == 0 )
return false;
int top = (int)Math.sqrt(num) + 1;
for(int i = 3; i < top; i+=2)
if(num % i == 0)
return false;
return true;
}
}
此代码计算一个数的最大素因子。 这给出了在线法官 5/6 次的错误。我无法弄清楚异常或错误。 请帮忙...
【问题讨论】:
-
你看过它给出的输出了吗?
-
你看过输入吗?
-
@Carcigenicate 它不显示输入和输出。只是时间
-
@PeterLawrey 它不显示输入和输出。只是时间
-
@Shadow_Sphynx 这就是插入打印语句或使用调试器的原因。您需要通过查看程序运行的情况来学习诊断程序。