简体   繁体   English

如何使用动态输入值实现以下输出?

[英]How can I Achieve the following output with dynamic input values?

In my office there was a jackpot programming event was conducted, in that event they asked 3 questions, but 1 of the puzzles was really tough for us and I just tried in my own interest在我的办公室进行了一次大奖编程活动,在那次活动中,他们问了 3 个问题,但其中一个谜题对我们来说真的很难,我只是为了自己的利益而尝试

Question:问题:

A = {10, 15, 25}
B = {1, 5, 20, 30}

Expected output:预期输出:

10 20
10 20 25 30
10 30
15 20
15 20 25 30
15 30
25 30

In output:在输出中:

  • 10 20 --> A's 1st element and B's 1st smallest element that greater than A's 1st element 10 20 --> A 的第一个元素和 B 的第一个最小元素,大于 A 的第一个元素

  • 10 20 25 30 --> A's 1st element Check with B array, which is greater than A, and again Check with A , repeat it until B doesn't have any greater element in compared to A, if B doesn't have to end up with previous B value. 10 20 25 30 --> A的第一个元素检查B数组,它大于A,然后再次检查A,重复它直到B没有比A更大的元素,如果B不需要以之前的 B 值结束。

  • 10 30 --> A's 1st element and B's largest element that greater than A's 1st element 10 30 --> A 的第一个元素和 B 的大于 A 的第一个元素的最大元素

the above method will iterate all over the A elements.上述方法将遍历所有 A 元素。

Here is my solution, the only thing that didn't make sense was why he skipped 15 in the first set of sets, and since you don't have any more information I had to suppose the reason for skipping it(call it an exception)这是我的解决方案,唯一没有意义的是为什么他在第一组中跳过了 15 个,并且由于您没有更多信息,我不得不假设跳过它的原因(称之为例外)

int[] A = { 10, 15, 25 };
int[] B = { 1, 5, 20, 30 };
//10 20
//10 20 25 30
//10 30
//15 20
//15 20 25 30
//15 30
//25 30

var result = A.SelectMany(x => GetIllogicalPermutations(x, A, B)).DistinctBy(x => x.Sum());
for (int i = 0; i < result.Count(); i++)
{
    Console.WriteLine(string.Join(' ', result.ElementAt(i).Select(x => x.ToString())));
}

Console.ReadLine();

static IEnumerable<int[]> GetIllogicalPermutations(int item, int[] setA, int[] setB)
{
    yield return new int[] { item, setB.Where(x => x > item).Min() };
    yield return setA.Where(x => x > item && x != (setA.Max() - setA.Min())).Concat(setB.Where(x => x > item)).Prepend(item).OrderBy(x => x).ToArray();
    yield return new int[] { item, setB.Where(x => x > item).Max() };
}

If I understood your problem correctly it is like this如果我正确理解了您的问题,就像这样

class Solution
{
    int[] a;
    int[] b;

    public Solution(int[] a, int[] b)
    {
        this.a = a;
        this.b = b;
    }

    void InterateA(int min, string output)
    {
        foreach (var a in a.OrderBy(n => n).SkipWhile(n => n <= min))
        {
            InterateB(a, $"{output}\t{a}");
        }
    }

    void InterateB(int min, string output)
    {
        foreach (var b in b.OrderBy(n => n).SkipWhile(n => n <= min))
        {
            var str = $"{output} {b}";
            Console.WriteLine(str);
            InterateA(b, str);
        }
        output = null;
    }

    public void Print()
    {
        InterateA(a.OrderBy(n => n).First() - 1, null);
    }
}

Test code测试代码

static void Main(string[] args)
{
    var a = new int[] { 10, 15, 25, 35 };
    var b = new int[] { 1, 5, 20, 30, 40 };
    var solution = new Solution(a, b);
    solution.Print();
    Console.ReadKey();
}

Performance is minimum as this is initial trivial solution and canbe optimized if it does the job correctly.性能是最低的,因为这是最初的微不足道的解决方案,如果它正确地完成了工作,可以对其进行优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM