4 Keys Keyboard in C++

C++Server Side ProgrammingProgramming

<p>Suppose we will try to write the letter &lsquo;A&rsquo;, using the keyboard. Our goal is to use only four keys and try to write maximum &lsquo;A&rsquo;s on the text field. The keys are &lsquo;A&rsquo;, &lsquo;C&rsquo;, &lsquo;V&rsquo;, and &lsquo;Ctrl&rsquo;.</p><p>To write a maximum number of A, we will use Ctrl + A to select All, Ctrl + C to copy, and Ctrl + V to paste.</p><p>So, if the input is like the number of keystrokes is 7 then the output will be 9 as of press A three times.</p><p>Then Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+V</p><p>To solve this, we will follow these steps &minus;</p><ul class="list"><li><p>if keyStrokes &lt;= 6, then</p><ul class="list"><li><p>return keyStrokes</p></li></ul></li><li><p>for n := 1 to 6, do</p><ul class="list"><li><p>result[n-1] := n</p></li></ul></li><li><p>for n := 7 to keyStrokes, do</p><ul class="list"><li><p>result[n-1] := 0</p></li><li><p>for breakpoint := n-3 down to 1, do</p><ul class="list"><li><p>curr := (n &ndash; breakpoint - 1)*result[breakpoint - 1]</p></li><li><p>if curr &gt; result[n-1], then</p><ul class="list"><li><p>result[n - 1] := curr</p></li></ul></li></ul></li></ul></li><li><p>result[keyStrokes - 1]</p></li></ul><h2>Example</h2><p>Let us see the following implementation to get a better understanding &minus;</p><p><a class="demo" href="http://tpcg.io/UBC80DOD" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include&lt;iostream&gt; using namespace std; int keyNumbers(int keystrokes){ //find number of &#39;A&#39;s using 4 types of keys &nbsp; &nbsp;if (keystrokes &lt;= 6) //if keystrokes are less than 7 &nbsp; &nbsp; &nbsp; return keystrokes; &nbsp; &nbsp; &nbsp; int result[keystrokes]; //store intermediate results &nbsp; &nbsp; &nbsp; for (int n=1; n&lt;=6; n++) //upto 6 keystrokes, we need that number of keystrokes for max &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result[n-1] = n; &nbsp; &nbsp; &nbsp; for (int n=7; n&lt;=keystrokes; n++){ //for 7th to higher result[n-1] = 0; //initially store 0 as result &nbsp; &nbsp; &nbsp; for (int breakPoint=n-3; breakPoint&gt;=1; breakPoint--){ //find breakpoint to select, copy and paste &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int curr = (n-breakPoint-1)*result[breakPoint-1]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (curr &gt; result[n-1]) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[n-1] = curr; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return result[keystrokes-1]; } int main(){ &nbsp; &nbsp;int keystrokes; &nbsp; &nbsp;cout &lt;&lt; &quot;Enter Number of keystrokes: &quot;; cin &gt;&gt; keystrokes; &nbsp; &nbsp;cout &lt;&lt; &quot;Maximum Number of A&#39;s with &quot;&lt;&lt;keystrokes &lt;&lt; &quot; keystrokes &nbsp; &nbsp;is: &quot;&lt;&lt; keyNumbers(keystrokes)&lt;&lt;endl; }</pre><h2>Input</h2><pre class="result notranslate">7</pre><h2>Output</h2><pre class="result notranslate">Enter Number of keystrokes: Maximum Number of A&#39;s with 0 keystrokes is: 0</pre>
Updated on 16-Nov-2020 14:36:11