繁体   English   中英

欧拉计划 Q #1

[英]Project Euler Q #1

C#项目欧拉

如果我们列出所有 10 以下的自然数是 3 或 5 的倍数,我们会得到 3、5、6 和 9。这些倍数之和是 23。

找出 1000 以下所有 3 或 5 的倍数之和。

当我运行我的代码时,我输入第一个多个“3”,然后输入第二个“5”,然后输出“1000”,但我得出的结论是,我的答案与真正的答案相差 1000,女巫是“233168” “我的代码在下面,我很想知道是否有人能看出哪里出了问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Find the sum of all the multiples of 3 or 5 below 1000.");
        Console.WriteLine("First Multiple: ");
        String Mult1 = Console.ReadLine();
        Console.WriteLine("Second Multiple:");
        String Mult2 = Console.ReadLine();
        Console.WriteLine("Out Of:");
        String outOfN = Console.ReadLine();
        int M1 = Int32.Parse(Mult1);
        int M2 = Int32.Parse(Mult2);
        int BN = Int32.Parse(outOfN);

        int MyResult1 = MyMathFunctions.FindMult(M1,M2, BN);
        
    
        Console.WriteLine("Your Answer is :" + MyResult1);
        Console.WriteLine("Answer should be: 233168");
        //Answer should be 233168


    }
    class MyMathFunctions
     {

        public static int FindMult(float M1,float M2, float BN)
        {
            int tot = 0;

            for(int i = 1 ; i <= BN; i++ )
                {
                   if( (i % M1 == 0) || (i % M2 == 0) )
                        {
                    tot += i;

                          }
           
                         }

                         return tot;
         }
      }
    }
 }

更仔细地阅读问题。

“找出 1000 以下所有 3 或 5 的倍数之和。”

无需包含数字“1000”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM