Print maximum number of A’s using given four keys in C++

Learn how to solve the “print maximum numbers of ‘A’ using four keys” problem in C++ in a very simple way.

Here in this program, we will try to print the maximum number of letter ‘A’, by using only four keys of the keyboard. The keys that we are going to use to get maximum A’s are ‘A’, ‘C’, ‘V’, and ‘Ctrl’. We will use select all, copy and paste. (CTRL+A, CTRL+C, CTRL+V)

Problem Statement:

Step 1: (A)     : Print one 'A' on screen.

Step 2: (Ctrl-A): Select the all A's present on the screen.

Step 3: (Ctrl-C): Copy it.

Step 4: (Ctrl-V): Paste it on screen, adding just after all the A's. Finally print it.

Few important points to discuss before writing program:

  • If  No of A < 7, then the output is no of A itself.
  • If No of A >=7, then in order to produce the desire sequence output, we will use the Ctrl-A, Ctrl-C and Ctrl-V.

Examples:

Input:  N = 3
Output: 3
We can produce the desire sequence output on screen by using
following key sequence.
A, A, A

Input:  N = 7
Output: 9
We can produce the desire sequence output on screen by using
following key sequence.
A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V

Program:

Now, let’s begin to write code for the maximum number of A using the above concept described –

#include<iostream>
using namespace std;

int find(int keystrokes)
{

    if (keystrokes < 7)  //if keystrokes are less than 7
        return keystrokes;
        int max = 0;   // Initializing the result 
    int breakpoint;
    for (breakpoint = keystrokes - 3; breakpoint >= 1; breakpoint--) {

        int curr = (keystrokes - breakpoint - 1) * find(breakpoint);
        if (curr > max)
            max = curr;
    }
    return max;
}

// Main program
int main()
{
    int keystrokes;
    for (keystrokes = 1; keystrokes <= 20; keystrokes++)
        cout<< "Maximum Number of A's with "<<keystrokes<<" keystrokes is" <<find(keystrokes);
        return 0;
}

Output:

The output of the above program is as follow-

Output 1:
Enter Number of keystrokes: 3
Maximum Number of A's with 3 keystrokes is: 3

Output 2:
Enter Number of keystrokes: 7
Maximum Number of A's with 7 keystrokes is: 9

Output 3:
Enter Number of keystrokes: 11
Maximum Number of A's with 11 keystrokes is: 27

I hope this will help you!

Thank you…

Priority Queue of Pairs in C++ with an example

Difference between exit() and _Exit() in C++

Leave a Reply