If we 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.
Find the sum of all the multiples of 3 or 5 below 1000.
我感觉还是将自己平时在Project Euler上做的题目整理一下,一是能记录自己的思路,同时也能将想法和别人分享,但我还是希望不要依赖于别人的题解,而努力地尝试自己得到答案,想必这样的收获才是巨大的.
这是道很简单的题目,其实用数学方法可以直接笔算出来:
-容斥原理+等差数列求和
用程序写的话也是非常简短的,在PE上做题的大多使用方便快捷的python做,我不会,因此只好用C++来写(原本想学Haskell的).
代码如下:(再次吐槽MarkDown的语法高亮)
#include<iostream>
using namespace std;
int main()
{
int ans = 0;
for(int i = 0; i < 1000; i++)
if(i % 3 || i % 5) ans += i;
cout << ans << endl;
return 0;
}