`
lxliio
  • 浏览: 855 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

生成{1,2,..n}的字典序r-组合算法

阅读更多

    public static void main(String[] args) throws IOException {
        int[] inputArray = {1, 2, 3, 4, 5, 6, 7, 8}; 
       
        int[] temp = {1,2,3,4};
        int total = 0;
     
       
        while(true){
            int change = temp.length-1;
           
            for(int i=0; i<temp.length; i++){
               
                if(temp[i] == inputArray[inputArray.length-temp.length+i]){
                    change = i-1;
                    break;
                }
            }
           
            temp[change] = temp[change]+1;
            int num = 1;
           
            for(int i=change+1; i<temp.length; i++){
                temp[i] = temp[change]+(num++);
            }
           
            System.out.println((++total)+": "+temp[0]+","+temp[1]+","+temp[2]+","+temp[3]);
           
            if(temp[0] == inputArray[inputArray.length-temp.length]){
                break;
            }
        }
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics