繁体   English   中英

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

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

我必须找到N之下3或5的所有倍数的总和。例如,如果我们必须列出10以下的所有自然数,它们是3或5的倍数,我们得到3、5、6和9,即这些倍数是23

现在剩下的唯一问题是我希望它能够读取所有数字,然后显示总和,因为现在它读取一个数字并在其后立即显示总和,有什么想法吗?

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);
}
}

我建议您使用Scanner 我也建议你添加到sum由迭代3n在增量3 然后,从5n增量为5 (然后排除的倍数3 ,因为他们已经被添加)。 就像是

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
        int n = scanner.nextInt();
        System.out.println(getSum(n));
    }
}

public static long getSum(int n) {
    long sum = 0;
    for (int i = 3; i < n; i += 3) {
        sum += i;
    }
    for (int i = 5; i < n; i += 5) {
        if (i % 3 != 0) { // <-- already added if i is divisible by 3
            sum += i;
        }
    }
    return sum;
}

根据下面的评论,将main更改为首先读取count的int ,然后将其存储在数组中。 就像是

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextInt()) {
        int toRead = scanner.nextInt();
        int[] vals = new int[toRead];
        for (int t = 0; t < toRead; t++) {
            if (scanner.hasNextInt()) {
                vals[t] = scanner.nextInt();
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int n : vals) {
            sb.append(getSum(n)).append(" ");
        }
        System.out.println(sb);
    }
}

将您的打印语句移出循环。

循环前

long[] sumArr = new long[Nbr];

内循环

sumArr[j] = Somme(Long.parseLong(numbers[j]));

循环后

for (long sum : sumArr) {
    System.out.println(sum);
}

如果您想先读取所有N,请先读取它们并将它们放入数组中,然后尝试求和

您可以制作ArrayList并将其总和添加到其中,然后在循环后将其打印出来

众所周知,我们需要将那些可被n分割为3或5的数字相加,其中n = 10或100或更大(这是极限)。 但是如果我们有n = 20,那么15(15%5 == 0 && 15%3 == 0)将会出现两次,因此我们只需要加一次就可以检查数字是否可以被15整除(因为5 * 3 = 15)。

   long n1=0,n2=0;
    int i=1,j=1;
    while(3*i<n)
        {
        n1 +=3*i;
        i++;
        if(5*j<n)
            {
            n2+=5*j;
            if(5*j%15==0)
                n2 -=5*j;
            j++;
        }
      }

    System.out.println(n1+n2);
    }

暂无
暂无

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

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