-1
C#項目歐拉項目歐拉,Q#1
如果我們在下面列出10是3或5的倍數的所有自然數,我們得到了3,5,6和9,這些倍數的總和23.
找到3所有倍數低於1000
總和或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;
}
}
}
}
難道不是
aw04
請妥善縮進並格式化你的代碼 - 如果不是因爲在堆棧溢出時詢問可讀的帖子,那麼對於你自己和未來可能的維護者(儘管這是一個'遊戲'問題,這是一個可怕的習慣)。 – Rob
爲什麼你在沒有做浮點數學時使用'float'? – nvoigt