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
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 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 30 --> A's 1st element and B's largest element that greater than A's 1st element
the above method will iterate all over the A elements.
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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.