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

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

在我的办公室举行了一次大奖编程活动,在那次活动中他们问了 3 个问题,但其中 1 个谜题对我们来说真的很难,我只是为了自己的兴趣尝试了一下

问题:

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

预期输出:

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

在输出中:

上述方法将遍历所有 A 元素。

这是我的解决方案,唯一没有意义的是为什么他在第一组中跳过了 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() };
}

如果我理解正确你的问题是这样的

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

测试代码

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

性能最低,因为这是最初的简单解决方案,如果正确完成工作,可以进行优化。