뱁새유니버스

[알고리즘10] 재귀호출 Branch와 Level을 입력받아 호출횟수 출력하기

by 뱁새유니버스

IT

Branch와 Level(깊이)를 입력받아. 몇번 재귀함수를 호출했는지 Count하는 알고리즘 코드입니다.

import java.util.Scanner;

//branch와 level을 입력받아 총 몇번의 재귀가 일어났는지 확인 문제

public class Main {
	
	static Scanner sc = new Scanner(System.in);
	static int N;
	static int branch;
	static int cnt;
	static void func(int index) {
		cnt++;
//    	System.out.print(index); // 출력: 현재 깊이
		if(index == N) {
			return;
		}
		   for(int i=0;i<branch;i++) {
			   func(index + 1); // 재귀 호출 1
		   }
        	return;
	}
		
	
	public static void main(String[] args) {
		branch = sc.nextInt();
		N = sc.nextInt(); // Level 깊이 

		func(0);
		System.out.println("총합"+cnt);
		
	}

}
반응형

블로그의 정보

가슴이 웅장해지는 모든것

뱁새유니버스

활동하기