Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 排列一个数组,使两个数字的平均值不在它们之间_Algorithm_Sorting_Math - Fatal编程技术网

Algorithm 排列一个数组,使两个数字的平均值不在它们之间

Algorithm 排列一个数组,使两个数字的平均值不在它们之间,algorithm,sorting,math,Algorithm,Sorting,Math,正如问题所说,找到一个算法来排列数组。这是一个Facebook采访问题 平均值需要精确。我们不走平均路线,走平均路线 编辑:引用一个例子,如果数字是1,2,5,9,那么排列{1,9,2,5}是有效的,但是{1,5,9,2}不是有效的,因为1和9的平均值是5,介于两者之间。除非没有有趣的事情(重复条目),初始检查表明这是有效的: void check(ref int x, ref int y, int z) { if ((x + z) / 2 == y)

正如问题所说,找到一个算法来排列数组。这是一个Facebook采访问题

平均值需要精确。我们不走平均路线,走平均路线

编辑:引用一个例子,如果数字是1,2,5,9,那么排列{1,9,2,5}是有效的,但是{1,5,9,2}不是有效的,因为1和9的平均值是5,介于两者之间。

除非没有有趣的事情(重复条目),初始检查表明这是有效的:

    void check(ref int x, ref int y, int z)
    {
        if ((x + z) / 2 == y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
    }

    int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    for( int i = 0; i < nums.Count() - 3; i++)
    {
        check(ref nums[i], ref nums[i + 1], nums[i + 2]);
    }

你必须回到开头,它仍然没有正确的答案。

被天鹰船长打败了,但是

Module Module1

    Sub Main()
        Dim a() As Integer = {9, 4, 7, 6, 4, 4, 3, 4, 1}
        Dim n = a.Length

        'TODO: check that sanity has a reasonable vale
        Dim sanity As Integer = n
        Dim ok As Boolean = False
        Dim temp As Integer

        While Not ok And sanity > 0
            ok = True
            For i = 0 To n - 3
                If ((a(i) + a(i + 2)) / 2) = a(i + 1) Then
                    temp = a(i)
                    a(i) = a(i + 1)
                    a(i + 1) = temp
                    ok = False
                End If

            Next

            sanity -= 1
        End While
        Console.WriteLine("OK: " & ok.ToString())

        Console.WriteLine(String.Join(" ", a))

        Console.ReadLine()

    End Sub

End Module

什么?我们必须以这种方式排列数组。2 3 4无效,因为(4+2)/2=3,2 4 3是有效的排列。1和11的平均值实际上是6,在您的示例中,1和11的平均值是6。您没有阅读[puzzle]标签描述是吗?@CaptainSkyhawk-Heh:)我似乎喜欢包含名为
sanity
的变量的算法。现在,当“在a和b之间”不是指“在a和b之间”,而是指“在a和b之间的某个时间间隔内”时,试试这个算法。现在,当“在a和b之间”不是指“在a和b之间”,而是“从a到b之间的某个地方”。问题更有趣。也更难。从未觉得这些问题适合面试。经常会有一些“技巧”会让你找到正确的答案。你可能会有一个非常有能力的程序员,而他却不懂你的“技巧”“然后你越过他/她。为了确保我没有编造东西,这里有一个从0到99的数字数组,正确排列:0 64 32 96 16 80 48 72 40 24 88 56 4 68 36 20 84 52 12 76 44 28 92 60 2 66 34 98 18 82 50 10 74 42 26 90 58 6 70 38 22 86 54 14 78 46 30 94 62 1 65 33 97 17 81 49 73 41 25 89 57 5 69 37 21 85 53 77 45 93 61 3 67 35 99 19 83 51 11 75 43 27 91 59 7 71 39 23 87 55 79 31 95 63。@n.m.我无法发现隐藏的东西按照你生成的序列排序,你能详细说明一下你是如何生成这个序列的吗?@Rohitchauhan好的,试着用二进制来看看这些数字,可能会更明显一些
Module Module1

    Sub Main()
        Dim a() As Integer = {9, 4, 7, 6, 4, 4, 3, 4, 1}
        Dim n = a.Length

        'TODO: check that sanity has a reasonable vale
        Dim sanity As Integer = n
        Dim ok As Boolean = False
        Dim temp As Integer

        While Not ok And sanity > 0
            ok = True
            For i = 0 To n - 3
                If ((a(i) + a(i + 2)) / 2) = a(i + 1) Then
                    temp = a(i)
                    a(i) = a(i + 1)
                    a(i + 1) = temp
                    ok = False
                End If

            Next

            sanity -= 1
        End While
        Console.WriteLine("OK: " & ok.ToString())

        Console.WriteLine(String.Join(" ", a))

        Console.ReadLine()

    End Sub

End Module