求 1000 以内所有 3 或 5 的倍数之和

发布于 2024-11-27 03:53:22 字数 550 浏览 1 评论 0原文

我正在用 C++ 解决欧拉项目上的问题,但我没有得到第一个问题的正确答案。

这是我的代码:

#include <iostream>

using namespace std;

int main()
{
    int b;
    int c;

    for (int a = 0; a <= 1000;)
    {
        a = a + 3;
        b = a + b;
    }
    cout << b << "\n";

    for (int a = 0; a <=1000;)
    {
        a = a + 5;
        c = a + c;
    }
    cout << c << "\n";

    b = b + c;
    cout << b << "\n";
    return 0;
}

我的输出是:

167835
101505
269340

我的逻辑错误在哪里?

I'm doing the problems on Project Euler in C++, but I'm not getting the right answers to the first one.

Here's my code:

#include <iostream>

using namespace std;

int main()
{
    int b;
    int c;

    for (int a = 0; a <= 1000;)
    {
        a = a + 3;
        b = a + b;
    }
    cout << b << "\n";

    for (int a = 0; a <=1000;)
    {
        a = a + 5;
        c = a + c;
    }
    cout << c << "\n";

    b = b + c;
    cout << b << "\n";
    return 0;
}

My output is:

167835
101505
269340

Where's the error in my logic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余生再见 2024-12-04 03:53:22

您将所有同时为 3 和 5 的倍数(即 15 的倍数)的值相加两次。此外,您还将包括 1002 和 1005,这可能不是有意的。

You are adding all values that are both multiples of 3 and 5 (i.e. multiples of 15) twice. Additionally, you will also include 1002 and 1005, which probably isn't intended.

别忘他 2024-12-04 03:53:22

您重复计算了 3 和 5 的倍数(即 15 的倍数)的数字。

You're double counting numbers that are multiples of 3 and 5 (i.e. multiples of 15).

肩上的翅膀 2024-12-04 03:53:22

考虑一下,求 3 到 20 的所有倍数之和?

答:=>

3, 6, 9, 12, 15 这是3到20的倍数

3到20的所有倍数的总和=> [3 + 6 +9 + 12 + 15]

(3 + 6 +9 + 12 + 15) 您可以按以下方式重写

3 (1+ 2+3 +4+5 ) = > 3(15)=> 45

序列的和可以使用以下公式计算:

K(K+1)/2=>这里 K 是 5 => 5 (5+1)/2 = >15

一般而言,我们可以说给定范围内任意数 (N) 的倍数 R

K = R/N;

N* (K (K+1))/2

在我们的例子中 R =20 且 N =3

int sumDivisibeBy(int R, int N)
{
   int K = R / N;
   int  SEQSUM =  ((K*(K + 1)) / 2));
   return (N*SEQSUM)
}

在你的例子中,你需要调用这个函数三次 =>

sumDivisibeBy(1000,3) + sumDivisibeBy(1000,5)-sumDivisibeBy(1000,15)

Consider, Find the sum of all multiples of 3 up to 20?

Ans : =>

3, 6, 9, 12, 15 this are multiples of 3 up to 20

Sum of all multiple of 3 up to 20 is => [3 + 6 +9 + 12 + 15]

(3 + 6 +9 + 12 + 15) you can rewrite in following way

3 (1+ 2+3 +4+5 ) = > 3 (15) => 45

sum of sequence can be calculated using following formula

K(K+1)/2 = > here K is 5 => 5 (5+1)/2 = >15

In general, We can say that multiple of any number (N) within given range R

K = R/N;

N* (K (K+1))/2

In our case R =20 and N =3

int sumDivisibeBy(int R, int N)
{
   int K = R / N;
   int  SEQSUM =  ((K*(K + 1)) / 2));
   return (N*SEQSUM)
}

In your case you need to call this function thrice =>

sumDivisibeBy(1000,3) + sumDivisibeBy(1000,5)-sumDivisibeBy(1000,15)

-残月青衣踏尘吟 2024-12-04 03:53:22

除了重复计算 15 的倍数之外,您的增量顺序也是错误的。如果您首先增加 a ,您的值将高于 1000。另外,我不确定 C++ 初始化 int 的情况,但也许将它们设置为等于 0,至少对于读者而言。

Along with double counting multiples of 15, your increments are in the wrong order. If you increment a first, you will have values above 1000. Also I'm not sure about c++ initializing ints, but maybe set them equal to 0, at least for readers.

荒岛晴空 2024-12-04 03:53:22

不会费心增加 3 和 5,您可以增加 1 并检查数字是否可以被 3 或 5 整除。计算机是为数字运算而设计的。

int sum = 0;
for (int i = 0; i < 1000; i++)
{
    if (i%3 == 0 || 
        i%5 == 0)
    {
        sum += i;
    }
}
cout << "SUM:" << sum << endl;

wouldn't bother incrementing by 3 and by 5, you can increment by 1 and check whether numbers are divisible by 3 or by 5. Computers are designed for number crunching.

int sum = 0;
for (int i = 0; i < 1000; i++)
{
    if (i%3 == 0 || 
        i%5 == 0)
    {
        sum += i;
    }
}
cout << "SUM:" << sum << endl;
难得心□动 2024-12-04 03:53:22

虽然其他人已经准确地发布了您的错误位置,但您也应该尝试找出您是如何得到错误答案的。

对于您的情况,您可以写出您确定为 3 的倍数和 5 的倍数的所有值;那么你就可以分析你应该看到的 333 个 3 的倍数和你应该看到的 199 个 5 的倍数。

我不想放弃寻找实际解决方案的关键(尽管其他人已经这样做了),但 PE 解决问题的一部分是调试。

While others have posted exactly where you've erred, you should be trying to figure out how you got the wrong answer as well.

In your case, you could have written all the values you determined to be multiples of 3 and multiples of 5; then you could have analyzed the 333 multiples of 3 you should've seen and the 199 multiples of 5 you should've seen.

I don't want to give away the keys to finding the actual solution (despite the fact that others have already) but part of the problem solving at PE is debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文