给定两个int数组,返回一个长度为2的数组,其中包含适合第一个和第二个数组的数组

时间:2023-02-06 12:14:29

I'm trying to solve the exercise bellow. I don't get any compiler errors.

我正试图解决下面的练习。我没有得到任何编译器错误。

When I run it though, in the main method only the first Make2 gets called and the program stops working with this error:

当我运行它时,在main方法中只调用第一个Make2并且程序停止使用此错误:

The program '[4864] Make2Two.vshost.exe' has exited with code -1073741510 (0xc000013a).

程序'[4864] Make2Two.vshost.exe'已退出,代码为-1073741510(0xc000013a)。

Could someone help me with that?

有人可以帮助我吗?

And is there any better way to solve the problem? I think I put too much unnecessary code.

有没有更好的方法来解决这个问题?我想我放了太多不必要的代码。

Thanks a lot.

非常感谢。

Problem: Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

问题:给定2个int数组,a和b,返回一个新的数组长度2,该数组长度2包含a中的元素,后跟b中的元素。阵列可以是任何长度,包括0,但是在2个阵列之间将有2个或更多个元素可用。

My code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Make2Two
{
    class Program
    {
        static void Main(string[] args)
        {
            Make2(new int[] { 4 }, new int[] { 4, 2, 3 }); //output 44
            Make2(new int[] { 2, 3 }, new int[] { 2, 3, 6 }); //the others are not running
            Make2(new int[] { 2, 5 }, new int[] { 7, 6, 5 });
        }
        public static int[] Make2(int[] a, int[] b)
        {
            int[] result = new int[2];
            if (a.Length >= 2)
            {
                for (int i = 0; i < 2; i++)
                {
                    result[i] = a[i];
                    Console.Write(result[i]);
                }
                Console.WriteLine();
                Console.ReadLine();
            }
            if (a.Length < 2)
            {
                for (int i = 0; i < 2; i++)
                {
                    result[0] = a[0];
                    if (i == 0)
                    {
                        for (int j = 1; j < 2; j++)
                        {
                            result[j] = b[j - 1];
                            for (int k = 0; k < result.Length; k++)
                            {
                                Console.Write(result[k]);
                            }
                        }
                        Console.WriteLine();
                        Console.ReadLine();
                    }
                }
            }
            return a;
        }
    }
}

2 个解决方案

#1


I do think you are overcomplicating it a little...

我认为你有点过于复杂......

And your code is wrong:

你的代码错了:

result[0] = a[0];

How can you be sure that a has at least one element? You should check it!

你怎么能确定a至少有一个元素?你应该检查一下!

Let's try in another way: three indexes: i (index of result), ai (index of a), bi (index of b). We always increment i. We increment ai or bi when we respectively take an element from a or from b.

让我们尝试另一种方式:三个索引:i(结果索引),ai(a的索引),bi(b的索引)。我们总是增加我。当我们分别从a或b中取一个元素时,我们递增ai或bi。

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    for (int i = 0, ai = 0, bi = 0; i < result.Length; i++)
    {
        if (ai < a.Length)
        {
            result[i] = a[ai];
            ai++;
        }
        else if (bi < b.Length)
        {
            result[i] = b[bi];
            bi++;
        }
        else
        {
            break;
        }

        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Another possible solution. Here we have two for cycles, one for a and one for b. The index i for result is shared.

另一种可能的解决这里我们有两个循环,一个用于a,一个用于b。结果的索引i是共享的。

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    int i = 0;

    for (int ai = 0; i < result.Length && ai < a.Length; i++, ai++)
    {
        result[i] = a[ai];
        Console.Write(result[i]);
    }

    for (int bi = 0; i < result.Length && bi < b.Length; i++, bi++)
    {
        result[i] = b[bi];
        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Addendum: if you want to check that your algorithm works, you should check corner cases like:

附录:如果要检查算法是否有效,则应检查以下情况:

Make2(new int[] { }, new int[] { 1, 2 }); // output 12
Make2(new int[] { 4, 5 }, new int[] { }); // output 45
Make2(new int[] { 1 }, new int[] { 5 }); // output 15

Note that in general, functions that do calculations and functions that write should be separate, so you should remove all the Console.Write from Make2 and put them in a separate method, that should be called by the Main method.

请注意,通常,执行计算的函数和写入的函数应该是分开的,因此您应该从Make2中删除所有Console.Write并将它们放在一个单独的方法中,该方法应该由Main方法调用。

#2


How about this?

这个怎么样?

int[] a = new int[5];
int[] b = new int[5];

var result = a.Concat (b).Take (2).ToArray();

PS: Would recommend reading about LINQ (https://msdn.microsoft.com/en-us/library/bb397897.aspx)

PS:建议阅读LINQ(https://msdn.microsoft.com/en-us/library/bb397897.aspx)

#1


I do think you are overcomplicating it a little...

我认为你有点过于复杂......

And your code is wrong:

你的代码错了:

result[0] = a[0];

How can you be sure that a has at least one element? You should check it!

你怎么能确定a至少有一个元素?你应该检查一下!

Let's try in another way: three indexes: i (index of result), ai (index of a), bi (index of b). We always increment i. We increment ai or bi when we respectively take an element from a or from b.

让我们尝试另一种方式:三个索引:i(结果索引),ai(a的索引),bi(b的索引)。我们总是增加我。当我们分别从a或b中取一个元素时,我们递增ai或bi。

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    for (int i = 0, ai = 0, bi = 0; i < result.Length; i++)
    {
        if (ai < a.Length)
        {
            result[i] = a[ai];
            ai++;
        }
        else if (bi < b.Length)
        {
            result[i] = b[bi];
            bi++;
        }
        else
        {
            break;
        }

        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Another possible solution. Here we have two for cycles, one for a and one for b. The index i for result is shared.

另一种可能的解决这里我们有两个循环,一个用于a,一个用于b。结果的索引i是共享的。

public static int[] Make2(int[] a, int[] b)
{
    int[] result = new int[2];

    int i = 0;

    for (int ai = 0; i < result.Length && ai < a.Length; i++, ai++)
    {
        result[i] = a[ai];
        Console.Write(result[i]);
    }

    for (int bi = 0; i < result.Length && bi < b.Length; i++, bi++)
    {
        result[i] = b[bi];
        Console.Write(result[i]);
    }

    Console.WriteLine();

    return result;
}

Addendum: if you want to check that your algorithm works, you should check corner cases like:

附录:如果要检查算法是否有效,则应检查以下情况:

Make2(new int[] { }, new int[] { 1, 2 }); // output 12
Make2(new int[] { 4, 5 }, new int[] { }); // output 45
Make2(new int[] { 1 }, new int[] { 5 }); // output 15

Note that in general, functions that do calculations and functions that write should be separate, so you should remove all the Console.Write from Make2 and put them in a separate method, that should be called by the Main method.

请注意,通常,执行计算的函数和写入的函数应该是分开的,因此您应该从Make2中删除所有Console.Write并将它们放在一个单独的方法中,该方法应该由Main方法调用。

#2


How about this?

这个怎么样?

int[] a = new int[5];
int[] b = new int[5];

var result = a.Concat (b).Take (2).ToArray();

PS: Would recommend reading about LINQ (https://msdn.microsoft.com/en-us/library/bb397897.aspx)

PS:建议阅读LINQ(https://msdn.microsoft.com/en-us/library/bb397897.aspx)