Advertisement
Guest User

Nested Recursion ACM Fun Problem 4744

a guest
May 3rd, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. abstract class Function{
  2.    String name;
  3.  
  4.  
  5.    public Function(String name) {
  6.        super();
  7.        this.name = name;
  8.    }
  9.  
  10.    @Override
  11.    public String toString() {
  12.        return name;
  13.    }
  14.  
  15.    abstract void apply();
  16. }
  17.  
  18. public class main {
  19.  
  20.    private static int P;
  21.    private static int a;
  22.    private static int b;
  23.    private static int n;
  24.    private static long count;
  25.  
  26.    static int indent = 0;
  27.  
  28.    static void find(final int n, final Function func){
  29.  
  30.        System.out.println(times(indent) + "find(" + n + "," + func + ")");
  31.        indent++;
  32.        if(n == 1){
  33.            for(int i = 0; i < a; i++) func.apply();
  34.        } else if (n == 2){
  35.            for(int i = 0; i < b; i++) func.apply();
  36.        } else {
  37.            find(n-1,new Function("find(" + (n-2) + "," + func.toString() + ")" ){
  38.  
  39.                @Override
  40.                public void apply() {
  41.                    find(n-2,func);
  42.                }
  43.  
  44.            });
  45.        }
  46.        indent--;
  47.    }
  48.  
  49.    private static String times(int indent2) {
  50.        String s = "";
  51.        for(int i = 0; i < indent2; i++){
  52.            s += " ";
  53.        }
  54.        return s;
  55.    }
  56.  
  57.    public static void main(String[] args) {
  58.        a = 4;
  59.        b = 5;
  60.        n = 5;
  61.        P = 13;
  62.        find(n, new Function("funny") {
  63.  
  64.            @Override
  65.            public void apply() {
  66.                count++;
  67.            }
  68.        });
  69.        System.out.println(count%P);
  70.    }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement