[英]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.