首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2013-12-04 
生成{1,2,..n}的字典序r-组合算法??? public static void main(String[] args) throws IOException {??? ?

生成{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;
??? ??? ??? }
??? ??? }
??? }

热点排行