根据一些规则在数组中的某个位置查找数字,在“数字”中添加复数

时间:2016-01-15 09:46:32

标签: c# arrays algorithm math queue

我是编程的初学者,我正在尝试编写一个应该解决以下数学问题的程序:

“在一个圆圈上我们有108个数字。任意20个连续数字的总和总是1000.在位置”1“我们有数字1,在位置”19“我们有数字19和位置”50“我们有50号。我们在“100”的位置上有多少个?“数字总是整数。

到目前为止,我知道如何使用像“for”这样的数组和循环结构。

我试图设置一个变量“check”,我将其初始化为“false”,以便稍后将其更改为true,然后我会在位置“100”上找到该数字。

我最初的想法是通过数组并将每个连续20个数字的总和。我使用了两个变量来为每个不是1,19或50的位置分配值,然后进行求和。我用两个“for”结构做了这个,一个在另一个里面。当然,这会产生一个问题,我无法正确计算前20个数字的值。

我在MSDN上检查了一些关于“队列”结构的信息,但我无法理解如何在我的特定情况下使用它。

如果有人对如何处理此问题有任何建议,请告诉我。 谢谢。

正如您所问,这是我到目前为止编写的代码。请注意我真的在编程的乞讨,我还有很多东西需要学习。

check = true;
int[] array = new int[108];

for (counter = 1; counter <= array.Length; counter++)
    {
        if (counter !=1 || counter !=19 || counter !=50)
        {
            for (i = 1; i <= 999; i++) 
            {
                array[counter] = i;
                if (counter >= 20) 
                {
                    for (consecutive = counter; consecutive => 2; consecutive--)
                    {
                        checkValue = array[consecutive] + array[consecutive]-1;
                    }
                    if (checkvalue != 1000) check = false;
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

正如@fryday所说,这个问题的代码并不代表任何没有逻辑的东西,所以在阅读本文之前你应该很好地理解解决方案。

让我向您解释这个问题的逻辑,然后阅读代码(您会看到这很简单)。首先你可能会注意到,每次连续获得20个数字时,下一个数字必须等于连续20个数字中的第一个,因为你有

  

x_1 + x_2 + ... + x_20 = 1000

  

x_2 + ... + x_20 + x_21 = 1000

所以

  

x_1 + x_2 + ... + x_20 = x_2 + ... + x_20 + x_21

最后

  

x_1 = x_21

根据这一见解,我们所要做的就是从这个位置1,19和50的每一个开始遍历数组,用这个值填充数组中的位置,然后计算下一个位置,将20加到当前和mod by 108,重复该过程,直到数组中的值分别等于1,19或50。然后数组中的值等于0意味着所有值必须具有相同的值(并且尚未找到)。为了找到该值,将20个值连续地求和,并将该差值除以1000,其值为等于0的元素的计数。您可以注意到位置100是其中之一,其值等于0.最后,我们得到值130.

这是代码:

using System;

namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = { 1, 19, 50 };
            int[] arr = new int[108];

            foreach (var val in values)
            {
                int pos = val - 1;
                while (arr[pos] != val)
                {
                    arr[pos] = val;

                    pos += 20;
                    pos %= 108;
                }
            }

            int sum = 0;
            int zeros = 0;
            for (int i = 0; i < 20; i++)
            {
                if (arr[i] == 0) zeros++;
                sum += arr[i];
            }

            Console.WriteLine((1000 - sum) / zeros);
        }
    }
}

答案 1 :(得分:1)

我用数组解决了这个问题,但最好使用某种类型的循环列表。这是关于结构选择。

一般情况下,你应该用逻辑解决这个问题,计算机只能帮助你检查结果或可视化数据。我将附加代码,显示我如何使用数组来解决这个问题:

N = 108
step = 20
a = [None] * (N + 1)
def toArrayIndex(i):
    if i < 0:
        return toArrayIndex(N + i)
    if i % N == 0:
        return N
    else:
        return i % N


def setKnownNumbers(value, index):
    while a[toArrayIndex(index)] != value:
        if a[toArrayIndex(index)] != None:
            print "ERROR: problem unsolvable i = %d" % toArrayIndex(index)
        a[toArrayIndex(index)] = value
        index += step

setKnownNumbers(1, 1)
setKnownNumbers(19, 19)
setKnownNumbers(50, 50)

现在你只需要了解如何完成它。这并不难;)

您可以使用该代码输出数组并检查正确性

# output 
for i in range(1,N + 1):
    print i, a[i]

counter = 0
for i in range(1,N + 1):
    s = 0
    for j in range(0,step):
        s += a[toArrayIndex(i + j)]
    if s == 1000:
        counter += 1

if counter == N:
    print "Correct"
print "a[100] = %d" % a[100]