Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
| Line 15: | Line 15: | ||
<syntaxhighlight lang="c" name="printa"> | <syntaxhighlight lang="c" name="printa"> | ||
#include <stdio.h> | #include <stdio.h> | ||
| − | + | ||
int main() | int main() | ||
{ | { | ||
| − | + | unsigned long N, M, i; | |
printf("Enter N: "); | printf("Enter N: "); | ||
| − | scanf("% | + | scanf("%lu", &N); |
if(N <= 6) | if(N <= 6) | ||
{ | { | ||
| Line 27: | Line 27: | ||
printf("A\n"); | printf("A\n"); | ||
} | } | ||
| − | + | ||
else if (N % 3 == 0) | else if (N % 3 == 0) | ||
{ | { | ||
| − | M = 3 * ( | + | 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++) | for(i = 0; i < 3; i++) | ||
printf("A\n"); | printf("A\n"); | ||
| Line 40: | Line 40: | ||
} | } | ||
} | } | ||
| − | + | ||
else | else | ||
{ | { | ||
| − | M = 4 * ( | + | M = 4 * (1L << (N-4)/3) + (N-4)%3; //1<<x gives pow(2,x) |
for(i = 0; i < 4; i++) | for(i = 0; i < 4; i++) | ||
printf("A\n"); | printf("A\n"); | ||
| − | + | ||
for(i=0; i < (N-4)/3; i++) | for(i=0; i < (N-4)/3; i++) | ||
{ | { | ||
| Line 53: | Line 53: | ||
printf("Paste\n"); | printf("Paste\n"); | ||
} | } | ||
| − | + | ||
for(i = 0; i < N%3; i++) | for(i = 0; i < N%3; i++) | ||
printf("A\n"); | printf("A\n"); | ||
} | } | ||
| − | printf("\nM = % | + | printf("\nM = %lu\n", M); |
} | } | ||
</syntaxhighlight> | </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>
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() {
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) 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 * (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>