如何转换代码以便找到所有可能的数字组合

时间:2013-06-04 16:15:22

标签: java recursion combinations

这是我到目前为止编写的代码,以便找到整数的唯一组合。 我收到了整数的总数和将要创建的团队的长度,我想使用递归来查找所有组合

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PartA {

    public static void main(String[] args) {
        Scanner sizeInput= new Scanner(System.in);    
        ArrayList<Integer> teams= new ArrayList<Integer>();
        int groupSize=0;
        int teamSize=0;
        System.out.println("Type the size you wish the main group to be: ");
        groupSize= sizeInput.nextInt();
        System.out.println(" The group will be of size : "+groupSize);
        System.out.println("Type the size the team will have:  ");
        teamSize= sizeInput.nextInt();
        System.out.println(" The team will be of size : "+teamSize);            
        ArrayList<Integer> mainGroup= new ArrayList<Integer>();

        for(Integer i=0;i<groupSize;i++){
            mainGroup.add(i); 

            int loopTimes=mainGroup.size();
            int count=0;
            showTeams(mainGroup,teams,loopTimes,count,teamSize,groupSize);
        }
    }

    public static Integer showTeams(ArrayList<Integer>mainGroup,
            ArrayList<Integer>teams,int loopTimes,int count,
                int teamSize,int groupSize) {
        int e=0;
        int i=0,o;
        //int count=0;
        //int loop=U.length();
        while(count<=2){

            for(i=0; i<loopTimes-1 ; i++){
                o=i+count;
                e=mainGroup.get(i)+mainGroup.get(o+1);
                System.out.println(e);
            }
            count++;
            loopTimes--;
            System.out.println(count + " " + loopTimes);

            if(count>loopTimes) {
                System.out.println("Solution Found");
                return e;
            }
            else
            {   System.out.println("Recursion Call");
                showTeams(mainGroup,teams,loopTimes,count,teamSize,groupSize);
            }
        }
        return e;
    }

}

我想要的只是打印整数的唯一组合

0 个答案:

没有答案
相关问题