簡體   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