Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
Line 27: | Line 27: | ||
printf("Enter N: "); | printf("Enter N: "); | ||
scanf("%lu", &N); | scanf("%lu", &N); | ||
+ | printf("\n\nKey Sequences:\n\n"); | ||
if(N <= 6) | if(N <= 6) | ||
{ | { | ||
Line 41: | Line 42: | ||
for(i = 1; i < N/3; i++) | for(i = 1; i < N/3; i++) | ||
{ | { | ||
− | printf(" | + | printf("\nCTRL+A\n"); |
− | printf(" | + | printf("CTRL+C\n"); |
− | printf(" | + | printf("CTRL+V\n\n"); |
} | } | ||
} | } | ||
Line 55: | Line 56: | ||
for(i=0; i < (N-4)/3; i++) | for(i=0; i < (N-4)/3; i++) | ||
{ | { | ||
− | printf(" | + | printf("\nCTRL+A\n"); |
− | printf(" | + | printf("CTRL+C\n"); |
− | printf(" | + | printf("CTRL+V\n\n"); |
} | } | ||
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).
<syntaxhighlight lang="c" name="printa">
int main() {
unsigned long N, M, i; printf("Enter N: "); scanf("%lu", &N); printf("\n\nKey Sequences:\n\n"); if(N <= 6) { M = 6; for(i = 0; i < N; i++) printf("A\n"); } else if (N % 3 == 0) { M = 3 * (1L<< (N-3)/3); //1<<x gives pow(2,x) and I'm too lazy to include math.h for(i = 0; i < 3; i++) printf("A\n"); for(i = 1; i < N/3; i++) { printf("\nCTRL+A\n"); printf("CTRL+C\n"); printf("CTRL+V\n\n"); } } else { M = 4 * (1L << (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("\nCTRL+A\n"); printf("CTRL+C\n"); printf("CTRL+V\n\n"); } for(i = 0; i < N%3; i++) printf("A\n"); } printf("\nM = %lu\n", M);
} </syntaxhighlight>
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).
<syntaxhighlight lang="c" name="printa">
int main() {
unsigned long N, M, i; printf("Enter N: "); scanf("%lu", &N); if(N <= 6) { M = 6; for(i = 0; i < N; i++) printf("A\n"); } else if (N % 3 == 0) { M = 3 * (1L<< (N-3)/3); //1<<x gives pow(2,x) and I'm too lazy to include math.h 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 * (1L << (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 = %lu\n", M);
} </syntaxhighlight>