Как вернуть значение из функции - PullRequest
0 голосов
/ 10 февраля 2020

Hy там,

Я очень новичок в программировании, у меня есть код, который может печатать все комбинации размера "k" из массива размера "n". но я хочу использовать эту информацию для дальнейших расчетов. Что я должен сделать, чтобы хранить эти комбинации в отдельных массивах? в частности, я хочу сохранить временный массив данных. т.е. комбинация {1,2,3}: {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}, я хочу хранить каждую комбинацию в массиве. Я надеюсь, что мой вопрос ясен.

Ниже приведен код, который я хочу изменить.

// C# program to print all  
// combination of size r  
// in an array of size n 
using System;

class GFG
{

    /* arr[] ---> Input Array 
    data[] ---> Temporary array to  
                store current combination 
    start & end ---> Staring and Ending  
                     indexes in arr[] 
    index ---> Current index in data[] 
    r ---> Size of a combination 
           to be printed */
    static void combinationUtil(int[] arr, int n,
                                int r, int index,
                                int[] data, int i)
    {
        // Current combination is ready 
        // to be printed, print it 
        if (index == r)
        {
            for (int j = 0; j < r; j++)
                Console.Write(data[j] + " ");
            Console.WriteLine("");
            return;
        }

        // When no more elements are  
        // there to put in data[] 
        if (i >= n)
            return;

        // current is included, put 
        // next at next location 
        data[index] = arr[i];
        combinationUtil(arr, n, r,
                        index + 1, data, i + 1);

        // current is excluded, replace 
        // it with next (Note that 
        // i+1 is passed, but index  
        // is not changed) 
        combinationUtil(arr, n, r, index,
                        data, i + 1);
    }

    // The main function that prints  
    // all combinations of size r 
    // in arr[] of size n. This  
    // function mainly uses combinationUtil() 
    static void printCombination(int[] arr,
                                 int n, int r)
    {
        // A temporary array to store 
        // all combination one by one 
        int[] data = new int[r];

        // Print all combination  
        // using temprary array 'data[]' 
        combinationUtil(arr, n, r, 0, data, 0);
    }

    // Driver Code 
    static public void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int r = 3;
        int n = arr.Length;
        printCombination(arr, n, r);
        Console.ReadKey();
    }
}

1 Ответ

0 голосов
/ 10 февраля 2020

Если вы хотите вернуть значение, вы можете использовать синтаксис этой функции: [тип] [имя функции] В этом случае кажется, что вы хотите вернуть массив целых чисел, поэтому вы можете написать:

int[] combinationUtil([members]){}

что-то вроде это. И вы можете собирать возвращаемые значения вне функции.

...