求算法

飞羊习习 2018-11-27 01:37:58
有48个不同的数,每五个一组,每个组不论顺序如何不能相同,例{0,1,2,3,4}和{4,3,2,1,0}视为相同的一个数组,输出所有不同的组合.
...全文
399 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞羊习习 2018-12-05
  • 打赏
  • 举报
回复
自发自解吧,我主要为了玩artifact哈哈
飞羊习习 2018-12-05
  • 打赏
  • 举报
回复
function combinations(arr, k){
var i,
subI,
ret = [],
sub,
next;
for(i = 0; i < arr.length; i++){
if(k === 1){
ret.push( [ arr[i] ] );
}else{
sub = combinations(arr.slice(i+1, arr.length), k-1);
for(subI = 0; subI < sub.length; subI++ ){
next = sub[subI];
next.unshift(arr[i]);
ret.push( next );
}
}
}
return ret;
}
combinations(["斧王","兽王","钢背兽","半人马战行者","勇者基弗","军团指挥官","梅兹","帕格纳","斯温","潮汐猎人","伐木机","熊战士","冰晶圣女","撼地者","智者吉姆伊","卡娜","露娜","米波","食人魔魔法师","殁境神蚀者","普瑞蕾斯","天怒法师","剧毒术士","宙斯","血魔","赏金猎人","诈者德比","巫妖","莱恩","瘟疫法师","幻影刺客","狙击手","索尔拉可汗","风暴之灵","修补匠","寒冬飞龙","亚巴顿","陈","黑暗贤者","卓尔游侠","魅惑魔女","梦者法瓦汗","狼人","马格纳斯","全知骑士","瑞克斯","树精卫士","冥界亚龙"], 5);
wsswm 2018-11-28
  • 打赏
  • 举报
回复
用循环语句个判断的嵌套,比较反锁,但是还是能解决的。
  • 打赏
  • 举报
回复
import java.util.ArrayList;
import java.util.List;

public class Test1 {

    public static void main(String[] args) {

        // 生成样例个数
        int m = 6;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            list.add(i);
        }

        List<List<Integer>> res = new ArrayList<>();
        List<Integer> curr = new ArrayList<>();
        // 每组个数
        int n = 5;
        dfs(res, n, 0, curr, list);

        res.forEach(System.out::println);
    }

    /**
     * dfs算法
     *
     * @param res   结果
     * @param n     每组个数
     * @param index 索引
     * @param curr  当前集合
     * @param nums  样例数组
     */
    private static void dfs(List<List<Integer>> res, int n, int index, List<Integer> curr, List<Integer> nums) {
        if (curr.size() == n) {
            res.add(new ArrayList<>(curr));
            return;
        }

        for (int i = index; i < nums.size(); i++) {
            curr.add(nums.get(i));
            dfs(res, n, i + 1, curr, nums);
            curr.remove(curr.get(curr.size() - 1));
        }
    }
}
输出结果 [0, 1, 2, 3, 4] [0, 1, 2, 3, 5] [0, 1, 2, 4, 5] [0, 1, 3, 4, 5] [0, 2, 3, 4, 5] [1, 2, 3, 4, 5] 注:跑48中挑5个的组合数,C(48,5) == 1712304
nayi_224 2018-11-27
  • 打赏
  • 举报
回复
with tab1 as (select level - 1 ll from dual connect by level <= 48)
select *
  from tab1 t1, tab1 t2, tab1 t3, tab1 t4, tab1 t5
 where t1.ll > t2.ll
   and t2.ll > t3.ll
   and t3.ll > t4.ll
   and t4.ll > t5.ll;

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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