Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public static int count(int units, int bars, int width) {
  2. int sum = 0;
  3. if (units >= 0 && memo[units][bars] != -1) //if the value has already been calculated return that value
  4. return memo[units][bars];
  5.  
  6. for (int i = 1; i <= width; ++i) {
  7. if (units == 0 && bars == 0)
  8. return 1;
  9. else if (bars == 0)
  10. return 0;
  11. else {
  12. sum += count(units - i, bars - 1, width);
  13. }
  14. }
  15. if (units > -1)
  16. memo[units][bars] = sum;
  17.  
  18. return sum;
  19. }
  20.  
  21. public static void main(String[] args) {
  22. Scanner in = new Scanner(System.in);
  23. //while (in.hasNext()) {
  24. int num = in.nextInt();
  25. int bars = in.nextInt();
  26. int width = in.nextInt();
  27. memo = new int[51][51];
  28. for (int i = 0; i < memo.length; ++i) {
  29. for (int j = 0; j < memo.length; ++j)
  30. memo[i][j] = -1;
  31. }
  32. int sum = 0;
  33. sum += count(num, bars, width);
  34. System.out.println(sum);
  35. //}
  36. in.close();
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement