c# - 将 C# 数组与自身进行比较

标签 c# .net arrays

我正在尝试解决这可能是一项简单的任务,但我对此非常陌生,并且不太了解如何以复杂的方式处理数组。我试图弄清楚两个输入是否每个对应的数字总和为相同的数字(例如,123 和 321、1+3、2+2 和 1+3 都等于 4)。

到目前为止,我的代码已将每个输入分解为数组,我可以将这些数组相加到第三个数组中,但我不知道如何检查它本身。我是否应该为第三个数组而烦恼,只是弄清楚如何在循环中检查数组的总和?

public static void Main()
{
    Console.Write("\n\n"); //begin user input
    Console.Write("Check whether each cooresponding digit in two intigers sum to the same number or not:\n");
    Console.Write("-------------------------------------------");
    Console.Write("\n\n");
    Console.Write("Input 1st number then hit enter: ");
    string int1 = (Console.ReadLine());//user input 1


    Console.Write("Input 2nd number: ");
    string int2 = (Console.ReadLine());//user input 2

    int[] numbers = new int[int1.ToString().Length]; //changing user inputs to strings for array
    int[] numbers2 = new int[int2.ToString().Length];
    for (int i = 0; i < numbers.Length; i++)
    {
        numbers[i] = int.Parse(int1.Substring(i, 1));//populating arrays
        numbers2[i] = int.Parse(int2.Substring(i, 1));
    }


    int[] numbers3 = new int[numbers.Length];

    for (int i = 0; i < numbers.Length; i++)
    {
        numbers3[i] = (numbers[i] + numbers2[i]);
    }
}

}

最佳答案

您可以即时创建集合...

bool isEqual = Console.ReadLine()
                      .ToCharArray()
                      .Select(i => Convert.ToInt32(i.ToString()))
                      .Zip(Console.ReadLine()
                                  .ToCharArray()
                                  .Select(i => Convert.ToInt32(i.ToString())),
                           (i, j) => new
                           {
                               First = i,
                               Second = j,
                               Total = i + j
                           })
                      .GroupBy(x => x.Total)
                      .Count() == 1;

如果所有元素加起来等于相同的值,输出将等于 true...

测试用例:

应该成功

12345
54321

应该失败

12345
55432

为了理解上面的查询,让我们把它分成几个部分。

// Here I'm just converting a string to an IEnumerable<int>, a collection of integers basically
IEnumerable<int> ints1 = Console.ReadLine()
                                .ToCharArray()
                                .Select(i => Convert.ToInt32(i.ToString()));

IEnumerable<int> ints2 = Console.ReadLine()
                                .ToCharArray()
                                .Select(i => Convert.ToInt32(i.ToString()));

// Zip brings together two arrays and iterates through both at the same time.
// I used an anonymous object to store the original values as well as the calculated ones
var zippedArrays = ints1.Zip(ints2, (i, j) => new
                                    {
                                        First = i,    // original value from ints1
                                        Second = j,   // original values from ints2
                                        Total = i + j // calculated value ints1[x] + ints2[x]
                                    });



// if the totals are [4,4,4], the method below will get rid of the duplicates.
// if the totals are [4,3,5], every element in that array would be returned
// if the totals are [4,4,5], only [4,5] would be returned.
var distinctByTotal = zippedArrays.GroupBy(x => x.Total);

// So what does this tell us? if the returned collection has a total count of 1 item,
// it means that every item in the collection must have had the same total sum
// So we can say that every element is equal if the response of our method == 1.

bool isEqual = distinctByTotal.Count() == 1;

关于c# - 将 C# 数组与自身进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51757840/

相关文章:

c# - 知道 DLL 在什么上下文中运行

绑定(bind)完成时的 .NET 事件

c - MISRA 9.2 初始化 float 和无符号数组

c++ - C++ 中的模板实例化

c# - UI 空闲时 iOS 后台线程变慢

c# - LINQ to Sql,无法对包含聚合或子查询的表达式执行聚合函数

asp.net - 常见的网络攻击

c# - 如何重写具有 out 参数的虚拟方法?

C# 将数组的元素移动到特定点,并将特定元素拉到前面

c++ - C++中数组的构造函数初始化列表