package com.yuzhouwan.hacker.algorithms;

import org.junit.Test;

/**
 * Copyright @ 2018 yuzhouwan.com
 * All right reserved.
 * Function：WKC Monkey
 *
 * @author Benedict Jin
 * @since 2018/1/10
 */
public class WKCMonkey {

    /*
        max: 0.99984, maxCount: 4.0, sum: 0.99984, count: 4.0
        pointBefore: 0, pointAfter: 0.99984
        max: 0.99984, maxCount: 4.0, sum: 4.9992, count: 20.0
        pointBefore: 4, pointAfter: 0.9992000000000001

        0.24996		0.24996		0.24996		4.24996
        0.24996		0.24996		1.24996		3.24996
        0.24996		0.24996		2.24996		2.24996
        0.24996		1.24996		1.24996		2.24996
        1.24996		1.24996		1.24996		1.24996
     */
    @Test
    public void bestChoice() {
        makeTheBestChoice("24996");
        makeTheBestChoice("35340");
    }

    private void makeTheBestChoice(String monkeyId) {
        Integer v = Integer.valueOf(monkeyId);
        double feed = v / (Math.pow(10, monkeyId.length()));
        double max = 0, sum = 0;
        int count = 0, maxCount = 0;
        int pointBefore;
        double pointAfter;
        String monkeyIdStr = feed + "";
        String monkeyIdPointAfterStr = monkeyIdStr.substring(1, monkeyIdStr.length());
        while (sum < 5) {
            count++;
            if ((sum += feed) > 5) break;
            pointAfter = sum - (pointBefore = (int) sum);
            if (pointAfter > max) {
                max = pointAfter;
                maxCount = count;
            }
            System.out.println(String.format("max: %s, maxCount: %s, sum: %s, count: %s", max, maxCount, sum, count));
            System.out.println(String.format("pointBefore: %s, pointAfter: %s\n", pointBefore, pointAfter));
        }
        String result = theBest(4, maxCount, 0, new StringBuffer(), new StringBuffer()).toString();
        String[] split = result.split(",");
        for (String s : split) {
            if (s == null || s.length() == 0) continue;
            String[] single = s.split(" ");
            for (String single_ : single) {
                System.out.print(single_ + monkeyIdPointAfterStr + "\t\t");
            }
            System.out.println();
        }
    }

    private static StringBuffer theBest(int n, int m, int start, StringBuffer path, StringBuffer result) {
        if (n == 0) {
            result.append(path).append(",");
        }
        if (start <= n && m == 1) {
            path.append(n);
            result.append(path).append(",");
            path.setLength(path.length() - 1);
            return result;
        }
        for (int i = start; i <= n - 1; i++) {
            theBest(n - i, m - 1, i, path.append(i).append(" "), result);
            path.setLength(path.length() - 2);
        }
        return result;
    }
}

@@ -59,16 +59,75 @@
 .Test;%0A%0A
+import static com.yuzhouwan.common.util.StrUtils.isBlank;%0A%0A
 /**%0A * C
@@ -279,16 +279,210 @@
 nkey %7B%0A%0A
+    private static final int MAX_FEED = 5;%0A    private static final int MAX_COUNT = 10;%0A    private static final String HIGH_SPLITTER = %22,%22;%0A    private static final String LOW_SPLITTER = %22 %22;%0A%0A
     /*%0A 
@@ -1035,13 +1035,14 @@
 ce(%22
-35340
+200004
 %22);%0A
@@ -1114,17 +1114,19 @@
     
-Integer v
+double feed
  = I
@@ -1153,33 +1153,8 @@
 yId)
-;%0A        double feed = v
  / (
@@ -1280,16 +1280,36 @@
 ntBefore
+, maxPointBefore = 0
 ;%0A      
@@ -1477,17 +1477,24 @@
  (sum %3C 
-5
+MAX_FEED
 ) %7B%0A    
@@ -1526,16 +1526,37 @@
     if (
+count %3E MAX_COUNT %7C%7C 
 (sum += 
@@ -1563,17 +1563,24 @@
 feed) %3E 
-5
+MAX_FEED
 ) break;
@@ -1670,24 +1670,70 @@
 er %3E max) %7B%0A
+                maxPointBefore = pointBefore;%0A
             
@@ -2072,17 +2072,45 @@
 theBest(
-4
+MAX_FEED - 1 - maxPointBefore
 , maxCou
@@ -2206,19 +2206,29 @@
 t.split(
-%22,%22
+HIGH_SPLITTER
 );%0A     
@@ -2275,36 +2275,18 @@
 if (
-s == null %7C%7C s.length() == 0
+isBlank(s)
 ) co
@@ -2331,19 +2331,28 @@
 s.split(
-%22 %22
+LOW_SPLITTER
 );%0A     
@@ -2370,23 +2370,23 @@
 (String 
+_
 single
-_
  : singl
@@ -2427,15 +2427,15 @@
 int(
+_
 single
-_
  + m
@@ -2520,24 +2520,60 @@
 ;%0A        %7D%0A
+        System.out.println(%22%5Cn%5Cn%22);%0A
     %7D%0A%0A    p
@@ -2693,30 +2693,16 @@
 (n == 0)
- %7B%0A           
  result.
@@ -2725,23 +2725,23 @@
 end(
-%22,%22);%0A        %7D
+HIGH_SPLITTER);
 %0A   
@@ -2840,19 +2840,29 @@
 .append(
-%22,%22
+HIGH_SPLITTER
 );%0A     
@@ -3046,19 +3046,28 @@
 .append(
-%22 %22
+LOW_SPLITTER
 ), resul

Improve WKCMonkey algorithm