<分区>
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
所以我决定尝试解决 Euler 站点上的一些问题,以提高编程水平。在尝试解决第一个问题时,我编写了一个快速代码来计算总和,它适用于 10 个数字,但对于 1000 个数字,它向我显示了答案:166833,这是错误的,我在这里找不到问题。如果有人可以给我提示以改进我的算法,那么它就会起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Multiplies_of_3_and_5
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[7000];
for (int j = 0; j<array.Length ;j++)
{
array[j] = j + 1;
}
int n = 1;
int z = 1;
int numbers = 0;
for (int i = 0; i<999; i++)
{
if (array[i] == 3 * n )
{
n++;
numbers += array[i];
}
else if (array[i] == 5 * z)
{
z++;
numbers += array[i];
}
}
Console.WriteLine(string.Join(" ", numbers));
Console.ReadLine();
}
}
}