package algo;
import java.util.Arrays;
public class CombPerm {
static int numbers[]={1,2,3,4,5} ;
static int answer[];
static boolean visit[];
public static void main(String[] args) {
// TODO Auto-generated method stub
answer=new int[2];
visit=new boolean[5];
comb(0,0);
System.out.println("++++++++++++++++++");
perm(0);
System.out.println("++++++++++++++++++");
comb2(0,0);
System.out.println("++++++++++++++++++");
perm2(0);
}
static void comb(int start, int cnt) { //조합
if(cnt==2) {
System.out.println(Arrays.toString(answer));
return;
}
for(int i=start; i<5; i++) {
answer[cnt]=numbers[i];
comb(i+1,cnt+1);
}
}
static void perm(int cnt) { //순열
if(cnt==2) {
System.out.println(Arrays.toString(answer));
return;
}
for(int i=0; i<5; i++) {
if(visit[i]==false) {
answer[cnt]=numbers[i];
visit[i]=true;
perm(cnt+1);
visit[i]=false;
}
}
}
static void comb2(int start, int cnt) { //중복조합
if(cnt==2) {
System.out.println(Arrays.toString(answer));
return;
}
for(int i=start; i<5; i++) {
answer[cnt]=numbers[i];
comb(i,cnt+1);
}
}
static void perm2(int cnt) { //중복순열
if(cnt==2) {
System.out.println(Arrays.toString(answer));
return;
}
for(int i=0; i<5; i++) {
answer[cnt]=numbers[i];
perm(cnt+1);
}
}
}
댓글