Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int count(int units, int bars, int width) {
- int sum = 0;
- if (units >= 0 && memo[units][bars] != -1) //if the value has already been calculated return that value
- return memo[units][bars];
- for (int i = 1; i <= width; ++i) {
- if (units == 0 && bars == 0)
- return 1;
- else if (bars == 0)
- return 0;
- else {
- sum += count(units - i, bars - 1, width);
- }
- }
- if (units > -1)
- memo[units][bars] = sum;
- return sum;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- //while (in.hasNext()) {
- int num = in.nextInt();
- int bars = in.nextInt();
- int width = in.nextInt();
- memo = new int[51][51];
- for (int i = 0; i < memo.length; ++i) {
- for (int j = 0; j < memo.length; ++j)
- memo[i][j] = -1;
- }
- int sum = 0;
- sum += count(num, bars, width);
- System.out.println(sum);
- //}
- in.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement