标签:contain first initial str line element most gen more
// C# program to find // combinations from n // arrays such that one // element from each // array is present using System; using System.Collections.Generic; class GFG{ // Function to print combinations // that contain one element from // each of the given arrays static void print(List<int> []arr) { // Number of arrays int n = arr.Length; // To keep track of next // element in each of // the n arrays int []indices = new int[n]; // Initialize with first // element‘s index for(int i = 0; i < n; i++) indices[i] = 0; while (true) { // Print current combination for(int i = 0; i < n; i++) Console.Write(arr[i][indices[i]] + " "); Console.WriteLine(); // Find the rightmost array // that has more elements // left after the current // element in that array int next = n - 1; while (next >= 0 && (indices[next] + 1 >= arr[next].Count)) next--; // No such array is found // so no more combinations left if (next < 0) return; // If found move to next // element in that array indices[next]++; // For all arrays to the right // of this array current index // again points to first element for(int i = next + 1; i < n; i++) indices[i] = 0; } } // Driver code public static void Main(String[] args) { // Initializing a vector // with 3 empty vectors List<int> []arr = new List<int>[3]; for(int i = 0; i < arr.Length; i++) arr[i] = new List<int>(); // Now entering data // [[1, 2, 3], [4], [5, 6]] arr[0].Add(1); arr[0].Add(2); arr[0].Add(3); arr[1].Add(4); arr[2].Add(5); arr[2].Add(6); print(arr); } } // This code is contributed by shikhasingrajput
标签:contain first initial str line element most gen more
原文地址:https://www.cnblogs.com/wkk2020/p/14225366.html