代码应循环遍历所有数字,直到找到两个可满足条件的数字:
数字因子必须包含原始数字的一半 因子数不能是100的倍数。因子数是T和H.
但是代码无效
import java.util.Scanner;
public class VampierSlayer {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.println("Input A Even Digit Integer");
int i = S.nextInt();
String iS = Integer.toString(i);
int t = 0;
int h = 0;
for (h = 0; h < 1000000000; h++) {
if (h * t == i && iS.length() / 2 == Integer.toString(h).length()
&& iS.length() / 2 == Integer.toString(t).length() && h % 100 == 1 && t % 100 == 1) {
System.err.println("Finish");
break;
} else {
t++;
h = h - 1;
}
if (t > 1000) {
t = 0;
h = h + 1;
}
System.out.println(t + " " + h);
}
System.out.println(Integer.toString(h) + "," + t);
}
}
答案 0 :(得分:1)
首先,你以一种不一致的方式迭代所有数字,你不会以这种方式获得所有可能的t-h
组合。更好的方法是使用2个嵌套的for循环:
for (int h = 0; h < ...; h++) {
for (int t = 0; t < ...; t++) {
...
}
}
接下来,您应该对模数如何工作进行一些研究,您应该
h % 100 == 0 && t % 100 == 0
正确检查数字t
和h
是100的倍数。
答案 1 :(得分:0)
“因子数不能是100的倍数。”这不意味着:
if (h * t == i && iS.length() / 2 == Integer.toString(h).length()
&& iS.length() / 2 == Integer.toString(t).length()
&& h % 100 != 0 && t % 100 != 0)