영상 버젼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package algo.youtube.a38;
 
import java.util.Scanner;
 
public class Main {
    
    public static void doFunc(int coupon, int stamp, int k, int ans) {
        if(coupon > 0) {
            coupon--;
            stamp++;
            ans++;
            doFunc(coupon, stamp, k, ans);
            return;
        } else if(stamp > 0) {
            if(stamp/> 0) {
                stamp -= k;
                coupon++;
                doFunc(coupon, stamp, k, ans);
            }
        }
        if(coupon == 0 && stamp/== 0) {
            System.out.println(ans);
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        for(int i = 0; i < 3; i++) {
            int n = scan.nextInt();
            int k = scan.nextInt();
            int coupon = n;
            doFunc(coupon, 0, k, 0);
        }
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

제 제귀 버젼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package algo.youtube.a38;
 
 
public class TaewonMain {
    
    public static int couponAmount(int chicken, int coupon) {
        int couponNum = chicken/coupon;
        int temp = 0;
        if(couponNum >= coupon) {
            temp = couponAmount(couponNum, coupon);
            
        }
        return  couponNum  + temp;
    }
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int[] list = new int[2];
        for(int i = 0; i < 3; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            list[0= Integer.parseInt(st.nextToken());
            list[1= Integer.parseInt(st.nextToken());
            int couponResult = couponAmount(list[0], list[1]);
            bw.write(list[0+ couponResult + "\n");
        }
        bw.flush();
        bw.close();
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

저는 쿠폰의 계수만 재귀로 구했는데 영상에서는 값도 같이 구했습니다. 같이 구하는걸 구현할려고 3시간동안 고민한 저한테는 쿠폰 계수만이라도 재귀로 얻을 수 있어서 만족합니다...

 

이것도 백준문제인데 제출하면 틀렸다고 뜨는데 재귀호출때문에 그런건지 아니면 안보이는 오류가 있는 것 같네요

'알고리즘' 카테고리의 다른 글

백준 5단계(2920)  (0) 2019.11.28
코테 11/25/19  (0) 2019.11.25
백준 5단계(2562)  (0) 2019.11.24
백준 5단계(10818)  (0) 2019.11.23
백준 4단계(11/23/19)(10952, 10951, 1110)  (0) 2019.11.23

+ Recent posts