java 排列组合

minsheng0825 2013-09-17 10:24:50

类似图片上的,很多查询条件,很多组合,请大神帮忙写一个排列组合程序,将查询条件的可能组合打印出来,谢谢了,分不多,请谅解!
...全文
124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
minsheng0825 2013-09-17
  • 打赏
  • 举报
回复
同时也谢谢其他几位的帮助!
minsheng0825 2013-09-17
  • 打赏
  • 举报
回复
引用 5 楼 ghostkngiht 的回复:
闲的无聊,写了一个,不知道结果对不对,仅供参考。

    public static void main(String[] args) {
        String[] array1 = new String[]{"汤……", "自……", "养……", "G……"};
        List<List<String>> l1 = sub(array1);
        String[] array2 = new String[]{"复……", "维……"};
        List<List<String>> l2 = sub(array2);
        String[] array3 = new String[]{"保……", "营……"};
        String[] array4 = new String[]{"国产", "进口"};
        for (int i = 0; i < array4.length; i++) {
            for (int j = 0; j < array3.length; j++) {
                for (int k = 0; k < l2.size(); k++) {
                    for (int l = 0; l < l1.size(); l++) {
                        System.out.println("p1 in " + l1.get(l));
                        System.out.println("p2 in " + l2.get(k));
                        System.out.println("p3=" + array3[j]);
                        System.out.println("p4=" + array4[i]);
                        System.out.println("******************");
                    }
                }
            }
        }
    }

    public static List<List<String>> sub(String[] array) {
        List<List<String>> list = new ArrayList<List<String>>();
        int size = (int) Math.pow(2, array.length);
        for (int i = 1; i < size; i++) {
            List<String> item = new ArrayList<String>();
            String str = String.format("%0" + array.length + "d", Integer.parseInt(Integer.toBinaryString(i)));
            for (int j = 0; j < array.length; j++) {
                if (str.charAt(j) == '1') {
                    item.add(array[j]);
                }
            }
            list.add(item);
        }
        return list;
    }
对的、这就是我想要的!
  • 打赏
  • 举报
回复
补充: 一、String flag="false"; String sql=null; if(品牌!=不限) { flag="true"; sql="select * from 表名 where 品牌=具体选的品牌"; } 二、if(类别!=不限) { if(flag=="true") { sql=" and 类别=具体选择的类别"; } else { flag="true" //标识设为真, sql="select * from 表名 where 类别=具体选择的类别"; } } 三、if(蓝帽!=不限) { ...//类似第二步 } 四、if(产地!=不限) { ...//类似第二步 } 若不符合题意,请补充说明!
ghostkngiht 2013-09-17
  • 打赏
  • 举报
回复
闲的无聊,写了一个,不知道结果对不对,仅供参考。

    public static void main(String[] args) {
        String[] array1 = new String[]{"汤……", "自……", "养……", "G……"};
        List<List<String>> l1 = sub(array1);
        String[] array2 = new String[]{"复……", "维……"};
        List<List<String>> l2 = sub(array2);
        String[] array3 = new String[]{"保……", "营……"};
        String[] array4 = new String[]{"国产", "进口"};
        for (int i = 0; i < array4.length; i++) {
            for (int j = 0; j < array3.length; j++) {
                for (int k = 0; k < l2.size(); k++) {
                    for (int l = 0; l < l1.size(); l++) {
                        System.out.println("p1 in " + l1.get(l));
                        System.out.println("p2 in " + l2.get(k));
                        System.out.println("p3=" + array3[j]);
                        System.out.println("p4=" + array4[i]);
                        System.out.println("******************");
                    }
                }
            }
        }
    }

    public static List<List<String>> sub(String[] array) {
        List<List<String>> list = new ArrayList<List<String>>();
        int size = (int) Math.pow(2, array.length);
        for (int i = 1; i < size; i++) {
            List<String> item = new ArrayList<String>();
            String str = String.format("%0" + array.length + "d", Integer.parseInt(Integer.toBinaryString(i)));
            for (int j = 0; j < array.length; j++) {
                if (str.charAt(j) == '1') {
                    item.add(array[j]);
                }
            }
            list.add(item);
        }
        return list;
    }
  • 打赏
  • 举报
回复
一、String flag="false"; String sql=null; if(品牌!=不限) { flag="true"; sql="select * from 表名 where 品牌=具体选的品牌"; } 二、if(类别!=不限) { if(flag=="true") { sql=" and 类别=具体选择的类别"; } else { sql="select * from 表名 where 类别=具体选择的类别"; } } 三、if(蓝帽!=不限) { ...//类似第二步 } 四、if(产地!=不限) { ...//类似第二步 } 若不符合题意,请补充说明!
minsheng0825 2013-09-17
  • 打赏
  • 举报
回复
引用 2 楼 rui888 的回复:
他们之间没有关系吗?
没有关系,他们是并列的
tony4geek 2013-09-17
  • 打赏
  • 举报
回复
他们之间没有关系吗?
失落夏天 2013-09-17
  • 打赏
  • 举报
回复
4个类别, 这个没什么好办法 4层for循环 至于多选的那个,就是C51+C52+C53+C54+C55种组合。求出该组合的所有可能为:http://blog.csdn.net/rzleilei/article/details/10461653 Cmn算法。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧