你可以从别人那里汲取某些思想,但必须|

ChrisNg

园龄:4年4个月粉丝:6关注:4

欧拉计划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。

本文作者:ChrisNg

本文链接:https://www.cnblogs.com/chrisng/p/15312221.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   ChrisNg  阅读(102)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起