コード例 #1
0
ファイル: Subset.cs プロジェクト: philstopford/DesignLibs_GPL
    public static void subset_by_size_next(int n, ref int[] a, ref int subsize, ref bool more,
                                           ref bool more2, ref int m, ref int m2)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    SUBSET_BY_SIZE_NEXT returns all subsets of an N set, in order of size.
    //
    //  Example:
    //
    //    N = 4:
    //
    //    1 2 3 4
    //    1 2 3
    //    1 2 4
    //    1 3 4
    //    1 3
    //    1 4
    //    2 3
    //    1
    //    2
    //    3
    //    (the empty set)
    //
    //  Discussion:
    //
    //    The subsets are returned in decreasing order of size, with the
    //    empty set last.
    //
    //    For a given size K, the K subsets are returned in lexicographic order.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 June 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the size of the set.
    //
    //    Input/output, int A[N].  The entries A(1:SUBSIZE) contain
    //    the elements of the subset.  The elements are given in ascending
    //    order.
    //
    //    Output, int &SUBSIZE, the number of elements in the subset.
    //
    //    Input/output, bool &MORE.  Set MORE = FALSE before first call
    //    for a new sequence of subsets.  It then is set and remains
    //    TRUE as long as the subset computed on this call is not the
    //    final one.  When the final subset is computed, MORE is set to
    //    FALSE as a signal that the computation is done.
    //
    //    Input/output, bool &MORE2, a variable for bookkeeping.
    //    The user should declare this variable, but need not initialize it.
    //    The output value from one call must be the input value for the next.
    //
    //    Input/output, int &M, &M2, variables for bookkeeping.
    //    The user should declare this variable, but need not initialize it.
    //    The output value from one call must be the input value for the next.
    //
    {
        switch (more)
        {
        case false:
            subsize = n;
            more    = true;
            more2   = false;
            m       = 0;
            m2      = 0;
            break;

        default:
        {
            switch (more2)
            {
            case false:
                subsize -= 1;
                break;
            }

            break;
        }
        }

        switch (subsize)
        {
        //
        //  Compute the next subset of size SUBSIZE.
        //
        case > 0:
            Ksub.ksub_next(n, subsize, ref a, ref more2, ref m, ref m2);
            break;

        case 0:
            more = false;
            break;
        }
    }
コード例 #2
0
    public static void ksub_next_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    KSUB_NEXT_TEST tests KSUB_NEXT.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    28 May 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int[] a = new int[3];
        int   i;

        Console.WriteLine("");
        Console.WriteLine("KSUB_NEXT_TEST");
        Console.WriteLine("  KSUB_NEXT generates all K subsets of an N set");
        Console.WriteLine("  in lexicographic order.");
        Console.WriteLine("");

        int rank = 0;

        const int n = 5;
        const int k = 3;

        for (i = 0; i < k; i++)
        {
            a[i] = 0;
        }

        bool more = false;
        int  m    = 0;
        int  m2   = 0;

        for (;;)
        {
            Ksub.ksub_next(n, k, ref a, ref more, ref m, ref m2);

            rank += 1;

            string cout = rank.ToString().PadLeft(4) + "    ";
            for (i = 0; i < k; i++)
            {
                cout += a[i].ToString().PadLeft(4);
            }

            Console.WriteLine(cout);

            if (!more)
            {
                break;
            }
        }
    }