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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章