Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从函数返回值_C#_Recursion_Functional Programming_Combinations - Fatal编程技术网

C# 如何从函数返回值

C# 如何从函数返回值,c#,recursion,functional-programming,combinations,C#,Recursion,Functional Programming,Combinations,好了 我对编程非常陌生,我有一个代码,可以从大小为“n”的数组中打印大小为“k”的所有组合。但我想用这些信息做进一步的计算。我应该如何将这些组合存储在单独的数组中?具体来说,我想存储临时“数据”数组。 i、 {1,2,3}的组合是{1}、{2}、{3}、{1,2}、{1,3}、{2,3}、{1,2,3} 我想将每个组合存储在一个数组中。 我希望我的问题是清楚的 下面是我要修改的代码 // C# program to print all // combination of size r /

好了

我对编程非常陌生,我有一个代码,可以从大小为“n”的数组中打印大小为“k”的所有组合。但我想用这些信息做进一步的计算。我应该如何将这些组合存储在单独的数组中?具体来说,我想存储临时“数据”数组。 i、 {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();
    }
}
/C#打印所有
//尺寸r的组合
//在大小为n的数组中
使用制度;
GFG类
{
/*arr[]-->输入阵列
data[]-->临时数组到
存储当前组合
开始和结束--->开始和结束
arr[]中的索引
索引--->数据中的当前索引[]
r--->组合的大小
印刷品*/
静态无效组合util(int[]arr,int n,
int r,int索引,
int[]数据,int i)
{
//当前组合已准备就绪
//要打印,请打印它
如果(索引==r)
{
对于(int j=0;j=n)
返回;
//电流包括在内
//下一个在下一个地点
数据[索引]=arr[i];
组合util(arr,n,r,
指数+1,数据,i+1);
//排除电流,请更换
//它与next(注意
//i+1已通过,但索引
//(未更改)
组合util(arr、n、r、index、,
数据,i+1);
}
//打印的主要功能
//大小为r的所有组合
//在arr[]中,尺寸为n。此
//函数主要使用combinationUtil()
静态无效打印组合(int[]arr,
整数n,整数r)
{
//要存储的临时数组
//一个接一个的组合
int[]数据=新的int[r];
//打印所有组合
//使用临时数组“数据[]”
组合util(arr,n,r,0,data,0);
}
//驱动程序代码
静态公共void Main()
{
int[]arr={1,2,3,4,5};
int r=3;
int n=阵列长度;
打印组合(arr、n、r);
Console.ReadKey();
}
}

如果要返回值,可以使用以下函数语法: [类型][功能名称] 在这种情况下,您似乎希望返回整数数组,以便可以编写:

int[] combinationUtil([members]){}
像这样的。
您可以在函数之外收集返回值。

只是对代码的一点小挑剔-实际上您不需要
n
,因为您在函数中从
数据中获得了它。Length
假设我是一个非常新的人,请更具体一些。它说不是所有的代码路径都返回一个值。我知道您想要获得一组数组,数据类型应该是int[],而不是int[]。