项目欧拉Q#1

时间:2016-03-28 01:23:03

标签: c#

C#Project Euler

  

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

     

查找低于1000的3或5的所有倍数的总和。

当我运行我的代码时,我输入第一个倍数" 3"那么我的第二个" 5"然后出去" 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;
         }
      }
    }
 }

1 个答案:

答案 0 :(得分:0)

更仔细地阅读问题。

&#34;求出低于1000的所有3或5的倍数之和。&#34;

无需包含数字&#34; 1000&#34;。

相关问题