Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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
Java 这些子集按什么顺序列出?_Java_Algorithm_Math_Subset_Discrete Mathematics - Fatal编程技术网

Java 这些子集按什么顺序列出?

Java 这些子集按什么顺序列出?,java,algorithm,math,subset,discrete-mathematics,Java,Algorithm,Math,Subset,Discrete Mathematics,我正在写一个程序来列出一个字符串的所有子集。我的程序(如下所示)按以下顺序列出了“abcd”的子集: '' 'd' 'c' 'cd' 'b' 'bd' 'bc' 'bcd' 'a' 'ad' 'ac' 'acd' 'ab' 'abd' 'abc' 'abcd' '' 'a' 'b' 'ab' 'c' 'ac' 'bc' 'abc' 'd' 'ad' 'bd' 'abd' 'cd' 'acd' 'bcd' 'abcd' 这是正确的。但是,参考解决方案按以下顺序列出: '' 'd' 'c' 'c

我正在写一个程序来列出一个字符串的所有子集。我的程序(如下所示)按以下顺序列出了“abcd”的子集:

'' 'd' 'c' 'cd' 'b' 'bd' 'bc' 'bcd' 'a' 'ad' 'ac' 'acd' 'ab' 'abd' 'abc' 'abcd'
'' 'a' 'b' 'ab' 'c' 'ac' 'bc' 'abc' 'd' 'ad' 'bd' 'abd' 'cd' 'acd' 'bcd' 'abcd'
这是正确的。但是,参考解决方案按以下顺序列出:

'' 'd' 'c' 'cd' 'b' 'bd' 'bc' 'bcd' 'a' 'ad' 'ac' 'acd' 'ab' 'abd' 'abc' 'abcd'
'' 'a' 'b' 'ab' 'c' 'ac' 'bc' 'abc' 'd' 'ad' 'bd' 'abd' 'cd' 'acd' 'bcd' 'abcd'
我的问题是:此订单的名称是什么?

以下是我的程序供参考:

import java.util.ArrayList;
import java.util.Collections;

/**
   This class generates subsets of a string.
*/
public class SubsetGenerator
{
   public static ArrayList<String> getSubsets(String word)
   {
       ArrayList<String> result = new ArrayList<String>();
      //fill out
       //result.add("");
       if(word.length() == 0)
       {

           result.add("");
        }

   else
    {
        String notFirst = word.substring(1);
        ArrayList<String> smaller = getSubsets(notFirst);
        //System.out.println(smaller);
        char first = word.charAt(0);

        result.addAll(smaller);

        for(String i: smaller)
        {
            result.add(first+i);
        }
    }


   //simpleSubsets = getSubsets(simple+word.charAt(0));

  // Form a simpler word by removing the first character
  // fill out

  // Generate all subsets of the simpler word
  // fill out

  // Add the removed character to the front of
  // each subset of the simpler word, and
  // also include the word without the removed character
  // fill out

  // Return all subsets
  return result;
   }
}
import java.util.ArrayList;
导入java.util.Collections;
/**
此类生成字符串的子集。
*/
公共类子生成器
{
公共静态ArrayList GetSubset(字符串字)
{
ArrayList结果=新建ArrayList();
//填写
//结果:添加(“”);
if(word.length()==0)
{
结果:添加(“”);
}
其他的
{
字符串notFirst=字。子字符串(1);
ArrayList较小=GetSubset(不是第一个);
//System.out.println(更小);
char first=word.charAt(0);
结果:addAll(较小);
用于(字符串i:更小)
{
结果。添加(第一个+i);
}
}
//simpleSubsets=getSubsets(simple+word.charAt(0));
//通过删除第一个字符形成一个更简单的单词
//填写
//生成更简单单词的所有子集
//填写
//将删除的字符添加到
//简单单词的每个子集,以及
//还包括不带删除字符的单词
//填写
//返回所有子集
返回结果;
}
}

如果你用二进制数数并将数字0和1转换为a、b、c和d,他们产生的顺序就是你得到的顺序:

d c b a | set
--------+----
0 0 0 0 | {}
0 0 0 1 | {a}
0 0 1 0 | {b}
0 0 1 1 | {a, b}
0 1 0 0 | {c}
0 1 0 1 | {a, c}
0 1 1 0 | {b, c}
0 1 1 1 | {a, b, c}
1 0 0 0 | {d}
1 0 0 1 | {a, d}
1 0 1 0 | {b, d}
1 0 1 1 | {a, b, d}
1 1 0 0 | {c, d}
1 1 0 1 | {a, c, d}
1 1 1 0 | {b, c, d}
1 1 1 1 | {a, b, c, d}

希望这有帮助

他们产生的顺序是,如果你用二进制数数并将数字0和1转换为a、b、c和d,你会得到的顺序:

d c b a | set
--------+----
0 0 0 0 | {}
0 0 0 1 | {a}
0 0 1 0 | {b}
0 0 1 1 | {a, b}
0 1 0 0 | {c}
0 1 0 1 | {a, c}
0 1 1 0 | {b, c}
0 1 1 1 | {a, b, c}
1 0 0 0 | {d}
1 0 0 1 | {a, d}
1 0 1 0 | {b, d}
1 0 1 1 | {a, b, d}
1 1 0 0 | {c, d}
1 1 0 1 | {a, c, d}
1 1 1 0 | {b, c, d}
1 1 1 1 | {a, b, c, d}

希望这有帮助

具体的顺序是什么?你的结果指的是什么?为什么你得到的正确输出是正确的?具体顺序是什么?你的结果指的是什么?为什么你得到的正确输出是正确的呢?我修复了我的程序,但我仍然无法理解二进制排序是如何工作的。我没有看到0和1的模式。你能再详细解释一下吗?我对这种排序很陌生,不过我记得我是在离散数学课上学的。非常感谢。“二进制排序就像十进制排序一样,真的,如果你缺少八个手指的话。”——汤姆·莱赫里修复了我的程序,但我仍然无法理解二进制排序是如何工作的。我没有看到0和1的模式。你能再详细解释一下吗?我对这种排序很陌生,不过我记得我是在离散数学课上学的。非常感谢。“二进制排序就像十进制排序一样,真的,如果你缺少八个手指的话。”——汤姆·莱勒