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

时间:2015-05-09 09:20:09

标签: c# arrays

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

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

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

有人可以帮我吗?

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

非常感谢。

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

我的代码:

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;
        }
    }
}

5 个答案:

答案 0 :(得分:3)

这个怎么样?

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

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

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

的内容

答案 1 :(得分:0)

我觉得你有点过分复杂......

你的代码错了:

result[0] = a[0];

如何确定a至少有一个元素?你应该检查一下!

让我们尝试另一种方式:三个索引:i(索引为result),ai(索引为a),bi(索引为b)。我们总是增加i。当我们分别从aibi获取元素时,我们会增加ab

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;
}

另一种可能的解决方案。这里我们有两个for周期,一个用于a,一个用于b。 <{1}}的索引i已共享。

result

附录:如果你想检查你的算法是否有效,你应该查看角落案例,如:

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;
}

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

答案 2 :(得分:0)

public int[] make2(int[] a, int[] b) {
  int[] arr=new int[2];


  if(a.length>=2){
    arr[0]=a[0];
    arr[1]=a[1];
    return arr;
  }
  if(a.length==1){
    arr[0]=a[0];
    arr[1]=b[0];
    return arr;

  }
  if(a.length==0){
    arr[0]=b[0];
    arr[1]=b[1];
    return arr;
  }
  return arr;




}

答案 3 :(得分:0)

public int[] make2(int[] a, int[] b) {
  int[] array = new int[2];
  
  int counter = 0;

 for(int i=0;i<a.length;i++)
 {
   if(counter < 2)
     array[counter++] = a[i];
 }
 for(int i=0;i<b.length;i++)
 {
   if(counter < 2)
     array[counter++] = b[i];
 }
  return array;
}

单独运行for循环以检查每个数组中的元素。

答案 4 :(得分:-2)

public int[] plusTwo(int[] a, int[] b)
{
    int res[]=new int[4];

    for(int i=0; i<2; i++)
    {
        res[i]=a[i];
        res[i+2]=b[i];
    }

    return res;
}