求和不等长数组 [英] Sum unequal length arrays

查看:84
本文介绍了求和不等长数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来验证和改进我的C#代码以添加单个整数个位数组元素。由于数字可能很大而不是由C#类型表示,因此决定将它们存储在整数数组中。

I need help to verify and improve my C# code to add individual integer single digit array elements. Since the numbers could be large not represented by C# types, it as decided to store them in integer arrays.

我有以下代码来执行此操作。我可以改进它吗?它适用于各种组合。

I have following code to do that. Can I improve it and is it working for various combinations please.

/*
Test Data and result:
int[] Aa =  { 1, 9, 5, 6};
int[] Bb =     { 9, 2, 4};
int[] ra = {0, 2, 8, 8, 0}

int[] Aa =    {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
int[] Bb =    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int[] ra = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0};

*/
        static void Main(string[] args)
        {
            // Sum two arrays of unequal lengths

            int[] Aa = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
            int[] Bb = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
 
            int[] TotalArray = SumArray(Aa, Bb);

            TotalArray = SumUnequalLengthArrays(Aa, Bb);

}

        static int[] SumUnequalLengthArrays(int[] arr1, int[] arr2)
        {
            // Add two corresponfing elemts of the arrays
            // Its needed to sum a really largre numbers not possible to store in default types
            // Add prefix zeroes to the shorter array to make it of the same length!
            // Then its easier to add w/o complications

            int carryOver = 0;

            // find length of larger of the two arrays
            int LongerLenth = ((arr1.Length >= arr2.Length) ? arr1.Length : arr2.Length);
            int offset = ((arr1.Length >= arr2.Length) ? arr1.Length - arr2.Length : arr2.Length - arr1.Length);

            int[] resultArray = new int[(LongerLenth + 1)];

            // Start summing two array elements backward!
            for (int j = LongerLenth - 1; j >= 0; j--)
            {
                // start from the last element
                int elemOne = GiveArrayElementOrZero(arr: arr1, arrIndex: j, maxLength: LongerLenth, offset: offset);
                int elemTwo = GiveArrayElementOrZero(arr: arr2, arrIndex: j, maxLength: LongerLenth, offset: offset);

                int sumOfTwoDigits = 0;

                int tmpDigitSum = elemOne + elemTwo + carryOver;

                if (tmpDigitSum > 9)
                {
                    carryOver = 1;
                    sumOfTwoDigits = tmpDigitSum - 10;
                }
                else
                {
                    sumOfTwoDigits = tmpDigitSum;
                    carryOver = 0;
                }

                // Assign the result array digit, had to adjust for the carryover digit position
                resultArray[j + 1] = sumOfTwoDigits;

            }

            // assign final carryOver digit
            resultArray[0] = carryOver;

            return resultArray;
        }

任何建议都会受到欢迎并提高我的编码技巧。谢谢,Piyush Varma

Any suggestion will be most welcome and improve my coding skill. Thank you, Piyush Varma

piyush varma

piyush varma

推荐答案

也许也考虑这种解决方案:

 


< span style ="font-size:9.5pt; font-family:Consolas; color:blue"> int [] Aa = {9,9,9,9,9,9,9,9,9,9,9};


int [] Bb = {         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
7,2,0,0,1};

int[] Bb = {                   7, 2, 0, 0, 1 };


 


var m = Math.Max(Aa.Length,Bb .Length);


 


var A = Enumerable.Repeat(0,m - Aa.Length).Concat( Aa);


var B = Enumerable.Repeat(0,m - Bb.Length).Concat(Bb);


 


var z = A.Zip(B,(a,b)=>
new {a,b})。Reverse();


 


var result = z.Aggregate(
new {r = Enumerable.Empty< int >(),
c = 0},


                          
(p,s)=>
new {r = prAppend((sa + sb +) pc)%10),

                          ( p, s ) => new { r = p.r.Append( ( s.a + s.b + p.c ) % 10 ),


<跨度>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;                       
c =(sa + sb + pc)/ 10});

                                            c = ( s.a + s.b + p.c ) / 10 } );


 


int [] TotalArray =(result.c == 1?


                     
result.r.Append(1) :

                     result.r.Append( 1 ) :


                     
result.r).Reverse()。ToArray();

                     result.r ).Reverse().ToArray();





这篇关于求和不等长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆