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

前端 未结 12 1101
我在风中等你
我在风中等你 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:47

    You are counting some numbers twice. What you have to do is add inside one for loop, and use an if-else statement where if you find multiples of 3, you do not count them in 5 as well.

     if(temp % 3 == 0){
         x.add(temp);
         totalforthree += temp;
     } else if(temp % 5 == 0){
         y.add(temp);
         totalforfive += temp;
     }
    

提交回复
热议问题