【问题标题】:How To Sort Subsets in Java如何在 Java 中对子集进行排序
【发布时间】:2015-05-07 22:47:53
【问题描述】:

亲爱的朋友们,我有一个任务,我几乎解决了它。但是我最近遇到了一个大问题,我两天都找不到出路。如果您能帮助我,我将不胜感激!

所以,假设用户输入了 5 (N) 我立即创建了这个序列以从中获取子集:{1,2,3,4,5}

如果 N = 4,则序列类似于:{1, 2, 3, 4} 等

下面的代码会生成所有类型的子集变体:

public static int[] genarator(int N)
{
    int[] generator = new int[(int) Math.pow(2, N)];
    int[] binDigit = new int[(int) Math.pow(2, N)];

    for (int i = 0; i < Math.pow(2, N); i++)
        generator[i] = (i >> 1) ^ i; // Right Shifting

    for (int i = 0; i < Math.pow(2, N); i++)
    {
        int one = 1;
        binDigit[i] = 0;
        while (generator[i] > 0)
        {
            binDigit[i] += (generator[i] % 2) * one;
            generator[i] /= 2;
            one = one * 10;
        }
    }

    return binDigit;
}

以及它返回结果的方式如下所示(在这种情况下:N = 4 {1, 2, 3, 4}):

1 
1 2 
2 
2 3 
1 2 3 
1 3 
3 
3 4 
1 3 4 
1 2 3 4 
2 3 4 
2 4 
1 2 4 
1 4 
4 

但是我的讲师希望我的程序按此顺序返回结果:

1
2
3
4
1 2
1 3
1 4
2 3
2 4
3 4
1 2 3
1 2 4
1 3 4
2 3 4
1 2 3 4

我现在使用TreeSet&lt;Long&gt; 和parseLong,所以我可以得到真实的结果,直到 1

回顾一下,我的问题是如何存储我从int[] genarator(int N) 获得的那些数字并像我的讲师要求的那样显示它们?

生成器的工作原理以及如何以错误的顺序获取数字?代码如下:

int N = read.nextInt();

        int[] sequence = new int[N];
        for (int i = 0; i < N; i++)
            sequence[i] = i + 1;

        int[] tempArray = new int[(int) Math.pow(2, N)];
        tempArray = genarator(N);

        for (int i = 1; i < Math.pow(2, N); i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (tempArray[i] % 10 == 1)
                {
                    System.out.print(sequence[j] + " ");
                }
                tempArray[i] /= 10;
            }
            System.out.println();
        }

感谢您的检查,对于这个太长的问题,我真的很抱歉。但我无法通过简短的解释来说明清楚。

【问题讨论】:

    标签: java subset


    【解决方案1】:

    您可以做的是创建一个可以与其他集合进行比较的集合抽象。请参阅Comparators 上的 Java 教程。

        //don't call this Set as there is already a Java Set 
        //interface that youdon't want to confuse yourself with   
        public class MySet implements Comparable<MySet>{
    
            int[] backingArray;
    
            public MySet(int n) {
               //initialize the Set
               this.backingArray = generator(n);
            }
    
            public static Int[] generator(int n) {
                //..whatever you do to generate your results
            }
    
            @Override
            public int compareTo(MySet otherSet) {
               //define how sets are compared (e.g what your professor is asking.  
               //In your example, if one set is shorter than another, 
               //it is considered 'smaller')
            }
    
        }
    
    Set<MySet> allSets = ....;
    

    只需调用Collections.sort(allSets);

    【讨论】:

    • 感谢您的回答。现在,我正在尝试改进我的代码,如上所示,并检查您共享的关于比较器的链接。但是我担心比较每个子集并使用这种方式对它们进行排序会超过时间限制。
    • 不幸的是,生成器似乎无法以这种方式正常工作。提示很多问题。我认为它需要一种不同的算法方法。有没有其他方法可以根据我当前的输出对它们进行排序?或者是否可以对当前输出实施此解决方案?
    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 2014-06-01
    • 2022-08-23
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多