Project Euler__problem 1

在一個師兄那裏看到在刷Project Euler

感到獸血沸騰,有種刷題的衝動

首先就比較簡單的第一題做起

Multiples of 3 and 5

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.


3的倍數和5的倍數

如果我們列出10以內所有3或5的倍數,我們將得到3、5、6和9,這些數的和是23。

求1000以內所有3或5的倍數的和。


首先求出1000以內的3與5的最大倍數

在對其求和剩餘提出來的3或5  再相加

編程也十分簡單





#include<iostream>

void fun(int n)
{
	int sum = 0, num;
	for (num = 1; num < n; num++)
		if (num%3==0||num%5==0)
		       sum += num;
	std::cout << "Output:" << sum << std::endl;
}

void main()
{
	fun(1000);
	system("pause");
}

可知結果是233168

發佈了33 篇原創文章 · 獲贊 13 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章