top of page
Caută
Poza scriitoruluioanaunciuleanu

How to print maximum number of A’s using given four keys in JAVA

Imagine you have a special keyboard with the following keys:

Key 1: Prints 'A' on screen

Key 2: (Ctrl-A): Select screen

Key 3: (Ctrl-C): Copy selection to buffer

Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.


If you can only press the keyboard for N times (with the above four keys), write a program to produce the maximum number of A's. The input is the number of times you can press the keyboard and the output is the maximum number of A’s that you can produce.


Input: int press = 11; Output: 27 Explanation: By pressing 11 times on the keyboard you get the maximum of 27 A’s on the screen with the following keyboard combinations: A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V, Ctrl A, Ctrl C, Ctrl V, Ctrl V


Solution:


1. static int getMaxLetters(int press) { 2. if (press <= 6) { 3. return press; 4. } 5. 6. int[] maxLetters = new int[press]; 7. 8. for (int i = 1; i <= 6; i++) { 9. maxLetters[i - 1] = i; 10. } 11. 12. for (int i = 7; i <= press; i++) { 13. maxLetters[i - 1] = Math.max(2 * maxLetters[i - 4], Math.max(3 * maxLetters[i - 5], 4 * maxLetters[i - 6])); 14. } 15. return maxLetters[press - 1]; 16. } 17.

2 afișări0 comentarii

Postări recente

Afișează-le pe toate

Tiling Problem in JAVA

You can use a board that is 2 x n size. The tiles are of size 2 x 1. Count the number of ways to tile the board. Tiles can be placed...

Comments


bottom of page