Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
| Line 2: | Line 2: | ||
A | A | ||
| + | |||
Ctrl+A | Ctrl+A | ||
Ctrl+C | Ctrl+C | ||
| + | |||
Ctrl+V | Ctrl+V | ||
| + | |||
where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” | where CTRL+A, CTRL+C, CTRL+V each acts as one function key for “Select All”, “Copy”, and “Paste” | ||
operations respectively. | operations respectively. | ||
| + | |||
If you can only press the keyboard for N times (with the above four keys), please write a program to produce | 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. | maximum numbers of A. If possible, please also print out the sequence of keys. | ||
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() {
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>