package land.study;

/**
 * Created by Thuc on 5/25/2016.
 * CONG_TRU_1
 */
public class CongTru1 {
    private int[] seeds = {27, 9, 3, 1, 0};

    public void solve() {
        for (int v = 40; v >=1; v--) {
            String s = f(v, 0);
            if (!s.endsWith("#")) System.out.println(v + " = " +  s.substring(3));
        }
    }

    private String f(int v, int i) {
        if (i >= seeds.length) return "#";

        if (v==0) return "";

        if (v == seeds[i]) return " + " + seeds[i];

        String cong = f(v-seeds[i], i + 1);
        if (!cong.endsWith("#")) return " + " + seeds[i] + cong;

        String tru = f(v+seeds[i], i + 1);
        if (!tru.endsWith("#")) return " - " + seeds[i] + tru;

        return f(v, i+1);
    }
}

@@ -268,16 +268,84 @@
 th(%22#%22))
+ %7B%0A                if (s.length() %3C= 5) s += %22 + 0%22;%0A               
  System.
@@ -386,16 +386,30 @@
 ng(3));%0A
+            %7D%0A
         

add + 0