的运动是:为什么我的代码有时无法按预期工作?
查找下面N.
我用的BigInteger这样我就可以处理大数字的3或5的所有倍数的总和。我通过了第一个测试用例,但通过提交,我只通过了六个测试用例中的一个。所以我的代码存在问题,但我不知道我在做什么错误。有人能帮我吗?谢谢。
这是我有:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Solution solution = new Solution();
for (int i = 0; i < 3; i++){
long input = scanner.nextLong();
BigInteger sums = new BigInteger("0");
sums = sums.add(solution.calculateSum(input));
if (sums.signum() == 1){
System.out.println(sums);
}
}
}
private BigInteger calculateSum(long input){
input--;
long totalElements = 0;
BigInteger sums = new BigInteger("0");
if (input >= 3){
totalElements = input/3;
sums = sums.add(BigInteger.valueOf((totalElements * (3 + totalElements *3))/2));
}
if (input >= 5){
totalElements = input/5;
sums = sums.add(BigInteger.valueOf((totalElements * (5 + totalElements *5))/2));
}
if (input >= 15){
totalElements = input/15;
sums = sums.subtract(BigInteger.valueOf((totalElements * (15 + totalElements *15))/2));
}
return sums;
}}
你的意思[问题1 - 项目欧拉(https://projecteuler.net/problem=1)?您不需要总结每个测试用例的所有答案。 – MikeCAT
谢谢你的回应。是的,我的意思是问题1.在我的代码中,我没有总结所有的测试用例,或者我做错了什么? – user3505506
如果(%)N%3 == 0,N%5 == 0,则需要查找模数函数。 – VeenarM