C# 使用递归添加字节数组

C# 使用递归添加字节数组,c#,C#,我需要编写一个函数,使用递归添加两个字节数组。此功能的测试如下所示: [Test] [TestCaseSource("Add_Function")] public AddResult Add_WithRecursiveAlgorithm(byte[] a, byte[] b) { // Arrange // Act var result = AddRecursion(a, b); // Assert return new AddResult(a, b, re

我需要编写一个函数,使用递归添加两个字节数组。此功能的测试如下所示:

[Test]
[TestCaseSource("Add_Function")]
 public AddResult Add_WithRecursiveAlgorithm(byte[] a, byte[] b)
 {
   // Arrange

   // Act
   var result = AddRecursion(a, b);

   // Assert
   return new AddResult(a, b, result);
  }
例如:

Input : { 1, 1, 1 }, { 1, 1, 1 }
Result: {2,2,2}

Input : { 1, 1, 255 }, {0, 0, 1 }
Result: {1,2,0}
一般规则如下:

•假设输入a和b从不为空,且始终为相同长度

•算法应对输入无破坏性

•算法应该能够处理大于1000的大输入长度,但输入永远不会大到足以导致堆栈溢出


有什么办法吗?

好的,你需要写的函数在哪里?你被困在哪里?这个问题是如何获得赞成票的?嗯,这很好,但你试过什么?你有太多的声誉,以至于你不知道so不是免费的代码编写服务。它不是代码编写服务,所以你需要提供你当前的尝试。这不是一个人们为你做作业的地方。顺便说一句,以下输入的结果应该是什么<代码>{250,50,130},{42000,180}?
public object[] Add_Function()
{
    return new object[]
    {
        new object[]
        {
            new byte[] { 1, 1, 1 }, new byte[] { 1, 1, 1 }, new byte[] { 2, 2, 2 }
        },
        new object[]
        {
            new byte[] { 1, 1, 255 }, new byte[] { 0, 0, 1 }, new byte[] { 1, 2, 0 }
        }
    };
}

[TestCaseSource("Add_Function")]
public void Add(byte[] a, byte[] b, byte[] expected)
{
    // arrange
    int len = Math.Max(a.Length, b.Length);
    Array.Resize(ref a, len);
    Array.Resize(ref b, len);
    byte[] result = new byte[len];

    //act
    DoAdd(a, b, result, 0, len - 1);

    //assert
    CollectionAssert.AreEqual(expected, result);
}

private void DoAdd(byte[] a, byte[] b, byte[] result, int broughtForward, int index)
{
    int carriedForward = (a[index] + b[index] + broughtForward) / (byte.MaxValue + 1);
    result[index] =(byte)((a[index] + b[index] + broughtForward) % (byte.MaxValue + 1));
    if (index > 0)
    {
        DoAdd(a,b,result, carriedForward, index-1);
    }
}