欧拉计划001--3或5的倍数

001欧拉计划001--3或5的倍数

首先,展示一下题目。
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\)的倍数之和。

第一题很简单,我们需要计算3或5的倍数,需要注意的是,if(i%3==0||i%5==0),我们用i%3,i%5来判断它是否为3和5的系数,考虑到题目中所说的是3或5的系数,那么我们只需要将条件改写成if(i%3==0||i%5==0)即可。这道题目我们用暴力循环即可解决。

#include <iostream>
using namespace std;

//暴力循环
int main(){
    int sum=0;
    for(int i = 1;i<1000;i++){
        if(i%3==0||i%5==0){
            sum = sum+i;
        }
    }
    cout<<sum<<endl;
    return 0;
}

最后算出来的结果是233168。

欧拉计划001--3或5的倍数

上一篇:Redis实现排行榜(带二位小数点)


下一篇:继承树追溯