find the sum of the multiples of 3 and 5 below 1000

前端 未结 12 1053
我在风中等你
我在风中等你 2021-01-18 08:04

Ok guys, so I\'m doing the Project Euler challenges and I can\'t believe I\'m stuck on the first challenge. I really can\'t see why I\'m getting the wrong answer despite my

12条回答
  •  耶瑟儿~
    2021-01-18 08:50

    public class Solution {
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while (t>0){
                int sum = 0;
                int count =0;
                int n = sc.nextInt();
                n--; 
                System.out.println((n/3*(6+(n/3-1)*3))/2 + (n/5*(10+(n/5-1)*5))/2 - (n/15*(30+(n/15-1)*15))/2);
                t--;
        }
    }
    }
    

提交回复
热议问题