230208 공부내용 정리

임준성·2023년 2월 8일
0

알고리즘

목록 보기
2/8

230208 공부내용 정리


주사위 던지는 경우의수 실습
package algo;

import java.util.Arrays;
import java.util.Scanner;

// 입력으로 주사위던지기 모드를 받는다.(1,2,3,4)
// 던지는 주사위 수도 입력받는다. ( 1<= N <= 10 )

public class Dice {
	
	static int N; // 던지는 주사위 수
	static int [] numbers; // 각각의 주사위의 눈
	static int totalCnt;  // 경우의 수
	static boolean[] isSelected;
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int mode = sc.nextInt();
		N = sc.nextInt();
		
		numbers = new int[N];
		
		switch (mode) {
		case 1: // 중복 순열
			dice1(0);
			break;

		case 2: // 순열
			isSelected = new boolean[7]; //자동초기화
			dice2(0);			
			break;
			
		case 3:
			dice3(0,1);
			break;
		
		case 4:
			dice4(0,1);
			break;
		}
		
		
		System.out.println("총 경우의수: "+totalCnt);

	}
	
	private static void dice1(int cnt) { // cnt : 기존까지 던져진 주사위 수 ==> 현재 주사위 수를 기록하기 위한 인덱스로 사용 
		
		if(cnt ==N) { // 던져진 주사위가 목표수에 도달하면 더 이상 던질 주사위 없음
			System.out.println(Arrays.toString(numbers));
			totalCnt++;
			return;
		}
		
		//주사위의 눈은 1~6의 경우가 있음
		for(int i=1; i<=6; i++) {
			numbers[cnt] =i;
			// 다음주사위 던지러 가기
			dice1(cnt+1);
		}
		
	}
	
	private static void dice2(int cnt) { // cnt : 기존까지 던져진 주사위 수 ==> 현재 주사위 수를 기록하기 위한 인덱스로 사용 
		
		if(cnt ==N) { // 던져진 주사위가 목표수에 도달하면 더 이상 던질 주사위 없음
			System.out.println(Arrays.toString(numbers));
			totalCnt++;
			return;
		}
		
		//주사위의 눈은 1~6의 경우가 있음
		for(int i=1; i<=6; i++) {
			//중복 체크
			if(isSelected[i])continue;
			numbers[cnt] =i;
			isSelected[i] = true;
			// 다음주사위 던지러 가기
			dice2(cnt+1);
			isSelected[i] = false;
		}
		
	}

	
	private static void dice3(int cnt, int start) { // cnt : 기존까지 던져진 주사위 수
													// start : 현재 주사위 눈의 시작
													// 원소 중복 가능, 구성형태는 다름
		if (cnt==N) {
			System.out.println(Arrays.toString(numbers));
			totalCnt++;
			return;
		}

		for (int i = start; i <= 6; i++) {
			numbers[cnt] = i;
			dice3(cnt+1,i);


			}
		
	}
	
	
	
	private static void dice4(int cnt, int start) { // cnt : 기존까지 던져진 주사위 수
													// start : 현재 주사위 눈의 시작
													// 원소 중복 불가능
		if (cnt==N) {
			System.out.println(Arrays.toString(numbers));
			totalCnt++;
			return;
		}
		
		for (int i = start; i <= 6; i++) {
			numbers[cnt] = i;
			dice4(cnt+1,i+1);
			
			
		}
		
	}

}



profile
아무띵크 있이

0개의 댓글