简体   繁体   English

N以下3或5的所有倍数的总和。

[英]The sum of all the multiples of 3 or 5 below N. Project Euler

So I'm doing the Project Euler challenge and I'm stuck at the first one, I'm using Java as pl. 因此,我正在进行Project Euler挑战,而我陷入了第一个挑战,我使用Java作为pl。 for example if we have to list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. We have to find the sum of all the multiples of 3 or 5 below N. 例如,如果我们必须列出所有低于10的自然数,这些自然数是3或5的倍数,我们得到3、5、6和9。这些倍数的总和为23。我们必须找到的所有倍数的总和。 N以下3或5。

My code works on Eclipse but I get "Nice try, but you did not pass this test case." 我的代码可以在Eclipse上运行,但是我得到了“很好的尝试,但您没有通过此测试用例”。 with stdout : No Response and when I submit the code I get Wrong Answer on all test cases, here's the code: 使用stdout:无响应,当我提交代码时,所有测试用例均得到错误答案,这是代码:

public class Solution {
    public static void main(String[] args) {
        for (int j = 0; j < args.length; j++) {
            int N = Integer.parseInt(args[j]);
            if (Somme(N) != 0) {
                System.out.println(Somme(N));
            }
        }
    }

    public static int Somme(int Nn) {
        int s = 0;
        for (int i = 0; i < Nn; i++) {
            if (((i % 3) == 0) || ((i % 5) == 0)
                && !(((i % 3) == 0) && ((i % 5) == 0))) {
                s = s + i;
            }
        }
        return (s);
    }
}

UPDATE : So, I looked more and it turns out that this is how it's supposed to be done: 更新:所以,我看上去更多了,原来这是应该做到的:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution{
public static void main(String[] args) throws IOException {


    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    int Nbr = Integer.parseInt(line);


        for(int j=0; j<Nbr;j++)
        {
            BufferedReader br2 = new BufferedReader(new   InputStreamReader(System.in));
            String line2 = br2.readLine();
            String[] numbers = new String[Nbr];
            numbers[j]= line2;
            System.out.println(Somme(Long.parseLong(numbers[j])));
        }

        }


public static long Somme(long Nn) {
    long s = 0;
    for (int i = 0; i < Nn; i++) {
        if (((i % 3) == 0) || ((i % 5) == 0)) {
            s = s + i;
        }
    }
    return (s);
}

} }

Now the only problem left is that I want it to be able to read ALL the numbers THEN display the sum, for now it reads one number and display the sum right after it, any ideas? 现在剩下的唯一问题是我希望它能够读取所有数字,然后显示总和,因为现在它读取一个数字并在其后立即显示总和,有什么想法吗?

You are skipping some numbers that should not be skipped. 您正在跳过一些不应跳过的数字。

if (((i % 3) == 0) || ((i % 5) == 0)
    && !(((i % 3) == 0) && ((i % 5) == 0)))

This statement says: i must be divisible by 3 or 5 AND is must not be divisible by 3 and 5 . 这句话说: i必须被35整除,并且不能被35整除。 Rephrased: i must be divisible by 3 or 5 , but not both of them. 改写: i必须被35整除,但不能同时被两个整除。 Just delete the second line and it should work. 只需删除第二行即可。

I believe it is a combination of what Turing85 said and wazaaaap. 我相信这是Turing85所说的和wazaaaap结合而成的。 The examples for Project Euler all show it does not take different inputs. 所有Euler项目的示例都表明,它并没有采取不同的投入。 You just have to produce the correct output. 您只需要产生正确的输出即可。 So replace Integer.parseInt(args[j]); 因此,替换Integer.parseInt(args[j]); with Integer.parseInt(1000); Integer.parseInt(1000); To add to what Turing said, the solution should follow the following psuedocode: 要添加图灵所说的内容,解决方案应遵循以下伪代码:

target=999
sum=0
for i=1 to target do
if (i mod 3=0) or (i mod 5)=0 then sum:=sum+i
output sum

Using a for loop, you can get all the numbers from 0 to 1000 , and by using an if condition, you get the required numbers ie, multiples of 3 , 5 , adding them gives the final output, like the following: 使用for循环,你可以得到所有从数字01000 ,并通过使用if条件,您获得所需的数,即,倍数35 ,加入他们给出了最终的输出,如下所示:

public class SUM_3_5{

    public static void main(String []args) {
        int sum=0;
        int i;

        for(i=0;i<1000;i++)
        {
             if(i%3==0||i%5==0)
                 sum=sum+i;
        }

        System.out.println(sum);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM