(Created page with "Imagine you have a special keyboard with the following keys: A Ctrl+A Ctrl+C Ctrl+V where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”...")
 
Line 13: Line 13:
 
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}===
 
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}===
  
<syntaxhighlight lang="c" name="largesum">
+
<syntaxhighlight lang="c" name="printa">
 
#include <stdio.h>
 
#include <stdio.h>
  
Line 59: Line 59:
 
     printf("\nM = %d\n", M);
 
     printf("\nM = %d\n", M);
 
}
 
}
 
+
</syntaxhighlight>
  
 
{{Template:FBD}}
 
{{Template:FBD}}
  
 
[[Category:Coding Questions for Placements]]
 
[[Category:Coding Questions for Placements]]

Revision as of 16:06, 17 June 2014

Imagine you have a special keyboard with the following keys: A Ctrl+A Ctrl+C Ctrl+V where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” operations respectively. If you can only press the keyboard for N times (with the above four keys), please write a program to produce maximum numbers of A. If possible, please also print out the sequence of keys. That is to say, the input parameter is N (No. of keys that you can press), the output is M (No. of As that you can produce).

Solution by Arjun Suresh

<syntaxhighlight lang="c" name="printa">

  1. include <stdio.h>

int main() {

   int N, M, i;
   printf("Enter N: ");
   scanf("%d", &N);
   if(N <= 6)
   {
       M = 6;
       for(i = 0; i < N; i++)
       printf("A\n");
   }
   else if (N % 3 == 0)
   {
       M = 3 * (1<< (2, (N-3)/3)); //1<<x gives pow(2,x)
       for(i = 0; i < 3; i++)
           printf("A\n");
       for(i = 1; i < N/3; i++)
       {
           printf("Select\n");
           printf("Copy\n");
           printf("Paste\n");
       }
   }
   else 
   {
       M = 4 * (1 << (2, (N-4)/3)) + (N-4)%3; //1<<x gives pow(2,x)
       for(i = 0; i < 4; i++)
           printf("A\n");
       for(i=0; i < (N-4)/3; i++)
       {
           printf("Select\n");
           printf("Copy\n");
           printf("Paste\n");
       }
   
       for(i = 0; i < N%3; i++)
           printf("A\n");
   } 
   printf("\nM = %d\n", M);

} </syntaxhighlight>




blog comments powered by Disqus

Imagine you have a special keyboard with the following keys: A Ctrl+A Ctrl+C Ctrl+V where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” operations respectively. If you can only press the keyboard for N times (with the above four keys), please write a program to produce maximum numbers of A. If possible, please also print out the sequence of keys. That is to say, the input parameter is N (No. of keys that you can press), the output is M (No. of As that you can produce).

Solution by Arjun Suresh[edit]

<syntaxhighlight lang="c" name="largesum">

  1. include <stdio.h>

int main() {

   int N, M, i;
   printf("Enter N: ");
   scanf("%d", &N);
   if(N <= 6)
   {
       M = 6;
       for(i = 0; i < N; i++)
       printf("A\n");
   }
   else if (N % 3 == 0)
   {
       M = 3 * (1<< (2, (N-3)/3)); //1<<x gives pow(2,x)
       for(i = 0; i < 3; i++)
           printf("A\n");
       for(i = 1; i < N/3; i++)
       {
           printf("Select\n");
           printf("Copy\n");
           printf("Paste\n");
       }
   }
   else 
   {
       M = 4 * (1 << (2, (N-4)/3)) + (N-4)%3; //1<<x gives pow(2,x)
       for(i = 0; i < 4; i++)
           printf("A\n");
       for(i=0; i < (N-4)/3; i++)
       {
           printf("Select\n");
           printf("Copy\n");
           printf("Paste\n");
       }
   
       for(i = 0; i < N%3; i++)
           printf("A\n");
   } 
   printf("\nM = %d\n", M);

}




blog comments powered by Disqus