Вы находитесь на странице: 1из 43

Call to Code 2014 Round 1 Sample Answers

and Solutions

April 2014

Contents
1 Logic Puzzles
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8

Smartphones . . . . . . .
Insert Correct Signs . . . .
Investigations . . . . . . .
Digital Clock . . . . . . .
Lucky and Unlucky Digits
Change . . . . . . . . . . .
Flowchart Simulation . . .
Codes and Ciphers . . . .

2 Programming Tasks
2.1
2.2
2.3
2.4
2.5
2.6

Dwarves and Coins . . .


Global Warming . . . . .
Rating . . . . . . . . . .
Almost Palindromes . .
Drawing Christmas Tree
Text Decompression . .

.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

. 2
. 3
. 5
. 7
. 8
. 9
. 11
. 14

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

18

18
21
25
29
32
37

Chapter 1
Logic Puzzles
1.1

Smartphones

Problem statement
This is a table that lists smartphones available for buying at some store.
ID
1
2
3
4
5
6
7
8
9
10
11
12

Manufactured in
China
USA
Germany
Germany
China
China
UK
Sweden
Germany
USA
Canada
UK

Screen size (inches)


4
5.3
5
6
4.8
5.3
6.3
4.7
5
5.1
5.4
6

Price (euro)
300
450
400
600
420
550
640
540
470
510
610
700

Test 1
Imagine that you are interested in smartphones that are manufactured in
Europe, have screen size of at least 5 inches and you have a limited budget
of 600 euro. How many options from the table will you have to consider?
2

Test 2
Find the most expensive smartphone with screen size of at most 5.5 inches
that is not manufactured in Europe. Write down the
textbfID (the leftmost column) of this smartphone.

Solutions
This puzzle is simple enough that you can just look at each row of the table,
and write down the ones matching the search conditions.
Answers to the rst test are shown bold in the table:
ID
1
2

Manufactured in
China
USA

Screen size (inches)


4
5.3

Price (euro)
300
450

5
6
7
8

China
China
UK
Sweden

4.8
5.3
6.3
4.7

420
550
640
540

USA
Canada
UK

5.1
5.4
6

3
4

10
11
12

Germany
Germany

5
6

Germany

400
600

470
510
610
700

The answer to the second test is 11.


1.2

Insert Correct Signs

Problem statement
Given an expression like
?1?2?3?5?8=3,
your task is to replace each ? with plus or minus so that the equation holds
true. For this example, you should write down:
+1+2+3+5-8=3
3

Test 1
Replace each ? with plus or minus so that the equation holds true:
?1?2?3?4?5?6?7=4

Test 2
Replace each ? with plus or minus so that the equation holds true:
?1?2?4?8?16?32?64=27

Solutions
Test 1
One insight that provides a useful way to attack the puzzle is looking at the
potential groupings of the number sequences provided. If you look at possible
arrangements of those numbers, you'll see that we can divide the numbers
into four groups and get sum of 1 in each group  for example: (+1), (-2+3),
(-4+5), (-6+7). So, one of the answers is +1-2+3-4+5-6+7.

Test 2
The maximum sum that can be obtained using numbers 1,2,4,8,16 and 32 is
63. (People who know binary are at an advantage here.) That means that
the largest value, 64, should go with a plus. And from the remaining values
we should get -(64-27) = -37. We can see that if some number X can be
obtained as a sum, then X also can be obtained (just replace + with - and
vice versa).
So, how to get 37 from 1,2,4,8,16 and 32? By similar logic, the largest
number, 32, should be paired with +, because the maximum value that we
can get with 1,2,4,8,16 is 31. Now we have to get 5 from 1,2,4,8 and 16. At
this step, we are left with just 5 numbers, so we can check all reasonable
combinations and nd out that 5 = 16 - 8 - 4 + 2 - 1. By adding 32, we get
37. By changing each sign to the opposite one, we get -37. And by adding
64 we get 27.

1.3

Investigations

Problem statement
In this task you are going to conduct several investigations.

Test 1
Somebody has broken the window in the classroom. There are three sus

pects: Aine,
Bebhinn and Caitlin. Aine
said that Bebhinn broke the window.
Bebhinn and Caitlin also said something, but nobody remembers what they
said. It is known that exactly one of the girls broke the window and only
this girl told the truth. Who broke the window?

Test 2
Somebody had written a very bad word on the whiteboard. There are ve
suspects. The crime is so serious that we will not reveal the names of the
suspects and will denote them as Suspect 1, Suspect 2, . . . Suspect 5. The
suspects said the following statements:

Suspect 1 said: Suspect 2 or Suspect 5 are at fault.


Suspect 2 said: It was denitely not me and not Suspect 4.
Suspect 3 said: Suspect 1 and 2, one of you is right and the other is
wrong.
Suspect 4 said: No, Suspect 3, you are wrong.
Suspect 5 didn't say anything.
It is known that exactly one of the suspects told the truth. Can you tell who
is guilty?

Solutions
Test 1
The proiblem is small enough that we can just work through the logic for all
the possible suspects.
5


What if Aine
broke the window? Then she told the truth. But she said
that Bebhinn is at fault. Contradiction!

What if Bebhinn broke the window? Then both Aine
and Bebhinn told
the truth, which is impossible.

So, it seems Caitlin broke the window. And indeed, there is no contradiction.

Test 2
Again, let's examine the logic of all the suspects' statements and see how
many suspects are telling the truth in each case.

What if Suspect 1 is at fault? Then:

 Suspect 1 is lying
 Suspect 2 is telling the truth
 Suspect 3 is telling the truth
That's enough  we have at least two suspects who are telling the
truth.

What if Suspect 2 is at fault? Then:

 Suspect 1 is telling the truth


 Suspect 2 is lying
 Suspect 3 is telling the truth
Again, we have two or more truth-tellers.

What if Suspect 3 is at fault? Then:

 Suspect 1 is lying
 Suspect 2 is telling the truth
 Suspect 3 is telling the truth
The same situation as in the rst case.

What if Suspect 4 is at fault? Then:


6






Suspect 1 is lying
Suspect 2 is lying
Suspect 3 is lying
Suspect 4 is telling the truth

So, the answer is found! It's Suspect 4 who committed the crime.
1.4

Digital Clock

Problem statement
A digital clock shows time like this  HH:MM.
The clock has two buttons which can change the current time. When the
rst button is pressed, the number of minutes is increased by 1. When the
second button is pressed, both minutes and hours are increased by 1. As a
special case, if the number of hours is 23 and we press the second button,
it will be 0 afterwards. Similarly, 59 minutes changes to 0 minutes after an
increase.
Examples:

If current time is 15:23 and the second button is pressed, time changes
to 16:24.
If current time is 22:59 and the rst button is pressed, time changes to
22:00 (not 23:00, the hours are not aected!).
If current time is 23:08 and the second button is pressed, time changes
to 00:09.
If current time is 23:59 and the second button is pressed, time changes
to 00:00.

Test 1
The clock shows 01:10. Write down the minimum number of pressings to
adjust the time shown to 02:18.

Test 2
The clock shows 23:57. Write down the minimum number of pressings to
adjust the time shown to 01:02.

Test 3
The clock shows 14:18. Write down the minimum number of pressings to
adjust the time shown to 06:00.

Solutions
The only way to change the number of hours is to press the second button.
So, let's calculate how many times we need to press it. As the number of
minutes changes too, add the number of pressings to the minutes. Then we
should use only the rst button to adjust the minutes.
Answers:
1. 8
2. 5
3. 42

1.5

Lucky and Unlucky Digits

Problem statement
Many people are sure that there is a mystic connection between numbers and
events that happen in our lives. Some people try to avoid certain numbers,
which are thought to be unlucky. A lot of people have favourite numbers,
which are believed to bring good luck. Although this is nothing more than a
tradition and it should not be taken seriously, it is often interesting to learn
the things people believe are connected to numbers.

Test 1

Aine
believes that 8 is an unlucky digit. She has just joined the school
football team and is heading to the rst training session. She knows that at
the rst session newcomers usually receive their numbers in the team. She
is afraid that she might get a number with the digit 8 in it. To estimate the

chance that it will not happen (and to calm Aine
down), calculate how many
numbers from 1 to 99 do not contain the digit 8.

Test 2
Caitlin believes that 4 and 7 are lucky digits. She loves numbers that only
consist of 4s and 7s, such as 44, 47, 74 or 747. Find out how many such
numbers are there amongs all the three-digit numbers (i.e. from 100 to 999).

Solutions
Test 1
There are 8 one-digit numbers without digit 8 (1,2,3,4,5,6,7,9). To nd out
how many two-digit numbers do not contain 8, we remind ourselves that
there are 8 possible rst digits (1,2,3,4,5,6,7,9) and 9 possible second digits
(0,1,2,3,4,5,6,7,9), so the total number is 8*9=72. Adding one-digit numbers,
we get 80.

Test 2
It's simple enough that we can just list all the numbers: 444, 447, 474, 744,
477, 747, 774, 777.
1.6

Change

Problem statement
Counting out change accurately is one of the very important duties of a
cashier. In this task, you will have to deal with a cash register with an
unlimited number of certain coins. You will be asked various questions about
the ways to give the change.

Test 1
A cashier has an unlimited number of 1-cent, 2-cent and 5-cent coins. What
is the minimum number of coins that she needs to get the sum of exactly 68
cents?

Test 2
A cashier has an unlimited number of 1-cent, 12-cent and 20-cent coins.
Of course, some of these coins do not exist in real life, but let's imagine for
this test that the cashier actually has only these kinds of coins. What is the
minimum number of coins that she needs to get the sum of exactly 65 cents?

Test 3
A cashier has an unlimited number of 1-cent, 2-cent and 5-cent coins. Imagine that the cashier wants to get, for example, 6 cents. There is more than
one way to do this:

take one 5-cent coin and one 1-cent coin (let's write it as 5+1);
take three 2-cent coins (2+2+2);
2+2+1+1
2+1+1+1+1
1+1+1+1+1+1
How many ways there are to get 13 cents? Write down the answer (a
single number).

Solutions
Test 1
For this set of coins a greedy algorithm can be applied  in other words,
when working towards some sum, take as many sub-units as you can towards
the goal for each unit. In this case, it would be taking as many 5-cent coins
as possible, then 2-cent ones and then one or zero 1-cent coins. This gives
us the answer of 15 coins in total (13*5+2+1).
10

Test 2
For this test a greedy algorithm gives us a solution with 8 coins in total (take
three 20-cent and ve 1-cent). Let's try to use fewer numbers of 20-cent
coins. If we take two 20-cent, we need to get the sum of 25 using 1-cent
and 12-cent coins. We can do this with three coins (two 12-cent and one
1-cent), which gives us a solution with 5 coins in total. It is easy to see that
by taking one or zero 20-cent coins we will have to use much more coins. So,
the optimal solution is to take two 20-cent coins, two 12-cent ones and one
1-cent coin, 5 coins in total.

Test 3
We need to nd a systematic way to list all variants. First, let us consider
how many 5-cent coins we take:

Two 5-cent coins. Then there are only two ways to get the sum of
3 using 1-cent and 2-cent coins: 2+1 and 1+1+1. So, add 2 to the
answer.
One 5-cent coins. We are left with the sum of 8. If we can use only 2cent and 1-cent coins, the only thing that makes the dierence between
the ways is the number of 2-cent coins used. As we can use 0,1,2,3 or
4 two-cent coins to get the sum of 8, add 5 to the answer.
No 5-cent coins. Similarly to the second case, we can see that are 7
ways to get the sum of 13 using 2-cent and 1-cent coins: take 0,1,2,3,4,5
or 6 two-cent coins.
Summing up all the cases we get the answer 14.
1.7

Flowchart Simulation

Problem statement
You have a owchart of an algorithm that takes a positive integer number
N , and produces some integer number as a result. You will have to simulate
this algorithm for several values of N .
In the owchart a DIV operation is used. It is called integer division and
means taking the integer part of the division. Let's clarify the meaning of N
11

DIV 10. When N is divided by 10, we get a number which can have fractional
= 48.3). The result of N
part (some digits after the decimal point, like in 483
10
DIV 10 is just the digits before the decimal point (48 in that case).
Let's consider more examples:
25 DIV 10 = 2
891 DIV 10 = 89
6 DIV 10 = 0

12

Test 1
What will be the output if N = 36?

Test 2
What will be the output if N = 983?

13

Test 3
What will be the output if N = 92190387?

Solutions
There are two ways to solve this. The rst one is to simulate the owchart
on each input value, which is at least simple, if mechanical  and too long!
The second way is to nd out what the owchart actually does, and then
to solve the tests, keeping that knowledge in mind.
The owchart contains two cycles. The inner one repeatedly divides N by
10 (using the DIV operation) until N becomes zero. Each time such division
is performed, the last digit of N is added to S . That means that after the
inner cycle variable S contains the sum of digits of N .
Let's take a look at the outer cycle. After the inner cycle, N is assigned
to S . It means that with each iteration of the outer cycle, N is replaced with
the sum of it's digits. This continues until N becomes a one-digit number,
which is printed as a result.
That's enough to be able to say the result without simulating the owchart.
Let's take the third test: 92190387. The sum of digits is 9+2+1+9+0+3+8+7=39.
Let's take the sum again: 3+9=12. And one more time: 1+2=3.
1.8

Codes and Ciphers

Problem statement
Test 1
Telegraphs were the text messages of the 19th century. Before the telegraph
was invented, it was impossible to get a message from North America to
Europe faster than ten days: the only way for information (or indeed anything
else) to travel was by boat. After it was invented, however, we could send
messages through a special cable in minutes rather than hours or days. The
rst telegraph cable between Canada and Europe was laid across the oor
of the Atlantic from Telegraph Field, Foilhommerum Bay, Valentia Island
in western Ireland to Heart's Content in eastern Newfoundland. It was rst
used on August 16, 1858.

14

The telegraph used Morse code to transmit the messages. In a Morse


code, each letter is represented as a unique sequence of dots and dashes. So,
if F is represented as , U is represented as and N is represented
as , the message FUN is encoded as
In this task you have to decode the morse-like code where C is represented
as , E as , S as , R as , T as .
Imagine that you received the encoded message
and you know that only letters C,E,R,S,T where used in the message. Your
task is to decode this message. Write down the original message below.

Test 2
Since the times of the ancient Rome, people have been trying to hide the
content of their messages from others by using dierent ciphers. Modern
ciphers are way too sophisticated to describe quickly, but if we look back in
the early ciphers, we can nd out that some of them were pretty easy.
One of the most popular types of ciphers was substitution ciphers. In a
substitution cipher, a rule is used to change each letter of the message, one
at a time. The rule says to replace (or substitute) each letter with another
letter from the alphabet. Probably, the earliest example of a substitution
cipher was Caesar cipher, which is named after Julius Caesar. In a Caesar
cipher, each letter is replaced by the letter that is three steps further in the
alphabet:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
DEFGHIJKLMNOPQRSTUVWXYZABC
Note that the alphabet is considered circular, so after the Z goes A. Using
this simple cipher, the message HELP ME PLEASE would be encoded as
KHOS PH SOHDVH.
Here is your task.
Paula was cursing, looking at her phone. Aideen leaned forward from the
glass of the bus shelter and asked her what was wrong. I got this weird
message from Jim, she said. She sounded puzzled. Show me, said Aideen.

HSMDS QGM DWXL QGMJ TSY ZWJW QWKLWJVSQ HDWSKW UGEW SFV HAUC AL MH A VG FGL
OSFL EQ HSJWFLK LG KWW LZAK EWKKSYW OZAUZ AK OZQ A SE KWFVAFY AL AF UGVW BAE

15

It's some sort of code, but I don't know what... said Paula. Aideen
looked at the screen, thought for a bit and said Does he usually sign his
texts?. Paula nodded. Well then, the last word is probably Jim, and the
rst word is probably Paula. That should give you a start.
Presuming Aideen is correct, can you decipher the rest of the message?

Solutions
Test 1
The rst letter in the message is clearly S, because no other letter is encoded
with three initial dots . The second letter is also determined uniquely:
it's E. The rest of the message is , and it seems it can
start both with E or with C. Let's suppose the next letter is E. Then the
fourth letter should be A, and we get stuck, because no letter is encoded with
two initial dashes . That means our assumption above was wrong, so the
third letter is C. The remaining message is . The next letter
is R for sure, and the last part ( ) can only be deciphered as ET. So,
the original message was SECRET.

Test 2
In this test we are given a substitution cipher. Let's use Aideen's guess and
make a correspondence between the letters in the rst and last words to their
codes:

HSMDS
PAULA

BAE
JIM

Now we know that H stands for P, S stands for A, and so on. Let's present
this knowledge as a table:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
IJ LM P
U
A
Now let's take a look at the word HDWSKW. We know some letters in
it: PL?A??, and we know that the third and the last letters are the same.
It seems that it is actually PLEASE! Let's check our guess. With our added
knowledge (W is E, K is S), let's decipher word EWKKSYW: MESSA?E.
What do you think it is? MESSAGE, of course! Let's add new entries to our
table:
16

ABCDEFGHIJKLMNOPQRSTUVWXYZ
IJ LM P S U
A E G
It looks like the letters in the second row come in the same order as in the
alphabet, but the order is shifted. In cryptography circles this is known as
a Caesar cipher; here a shift of 8 letters is used. This allows us to complete
the table:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
IJKLMNOPQRSTUVWXYZABCDEFGH
Now decipher the rest of the message and get the following:

PAULA YOU LEFT YOUR BAG HERE YESTERDAY PLEASE COME AND PICK IT UP I DO NOT
WANT MY PARENTS TO SEE THIS MESSAGE WHICH IS WHY I AM SENDING IT IN CODE JIM
It's true that you could guess that the cipher is a Caesar-like even earlier.
The introduction talks about the Caesar cipher, and that is no coincidence.

17

Chapter 2
Programming Tasks
2.1

Dwarves and Coins

Problem statement
For sure, you have heard about the love that pirates have for gold. Dwarven
pirates are even more avaricious. There are a lot of legends and fairy-tales
that describe the passion of these small creatures for this metal.
Let's imagine the situation where three dwarves have found a goblin's
chest with N gold coins and have to divide them. Due to ancient rules
governing the allocation of loot to pirates in order of seniority, the oldest
dwarf should get one coin more than the middle dwarf, and the middle dwarf
should get one coin more than the youngest dwarf.
Help the dwarves to divide the coins in this way, or tell them that this is
impossible.

Input
You are given integer N (1 N 1000).

Output
If it is impossible to divide the coins in the way that dwarves want, print
-1 (minus one). Otherwise, print the numbers of coins which the oldest, the
middle and the youngest dwarfs get (in this order). Separate the numbers
with spaces.
18

Examples
Input

3
9
7

2 1 0
4 3 2
-1

Output

Solutions
There are two things that we have to do in order to solve this problem. First,
we should understand how to divide the coins. And second, we have to code
that.
To perform the rst step, let's do some calculations. Let's denote the
number of coins the middle dwarf will get as X . The youngest dwarf should
get (X 1) coins, the oldest one should get (X + 1) coins. In general, it is
X + (X 1) + (X + 1) = 3 X . As long as the total number of coins is N ,
we have 3 X = N . From this we can see that

if N is not divisible by three, there is no way to distribute the coins as


desired;
if N is divisible by three, X is equal to N/3.
Now we are going to reect this ideas in code. The value of N is given to
our program in standard input (stdin). So, the rst thing to do is to learn
how to read a value from stdin in your language.
When we already have the value as a variable, we need to check whether
this value is divisible by three. All the allowed programming languages provide relevant arithmetic operators. For this problem, we are interested in the
operation that gives us the remainder after the division. In most languages,
this operator is the % character (i.e. N % 3 is equal to the remainder of
division the value of N by three). So, here we just check that the remainder
is equal to zero.
If N is divisible by zero, we can compute X as N divided by three. Again,
use the division operator in your programming language. In most languages
this is a forward slash (/).
Once X is computed, print the values (X + 1), X and (X 1), separating
them with spaces.
19

Python 2
n = i n t ( raw_input ( ) )
i f n % 3 != 0 :
p r i n t 1
else :
x = n / 3
print x + 1 , x , x 1

Python 3
n = i n t ( input () )
i f n % 3 != 0 :
p r i n t ( 1)
else :
x = n / 3
p r i n t ( "%d %d %d" % ( x + 1 , x , x 1 ) )

Java
import j a v a . u t i l . Scanner ;
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new Main ( ) . run ( ) ;
}
p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
i nt gold = input . nextInt () ;

i f ( g o l d % 3 != 0 ) {
System . out . p r i n t l n ( "1" ) ;
} else {
int part = gold / 3;
System . out . p r i n t ( ( p a r t + 1 ) + " " + p a r t + " " + ( p a r t 1 ) ) ;
}

C++
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
int n;
c i n >> n ;

20

i f ( n % 3 == 0 ) {
int x = n / 3;
c o u t << x + 1 << " " << x << " " << x 1 << e n d l ;
} else {
c o u t << 1 << e n d l ;
}
return 0;

JavaScript
var n = p a r s e I n t ( r e a d l i n e ( ) )
i f ( n % 3 == 0 ) {
var x = n / 3

p r i n t ( ( x + 1) + " " + x + " " + ( x 1) )


} else {
p r i n t ( 1)
}

PHP
<?php
$n = f g e t s (STDIN) ;
i f ( $n % 3 == 0 ) {
$x = $n / 3 ;
p r i n t ( $x + 1 ) . " " . $x . " " . ( $x 1 ) ;
} else {
p r i n t "1" ;
}
?>

2.2

Global Warming

Problem statement
A lot of scientists around the world are researching the global warming phenomena. One of the simplest models says: if this century the average temperature is T , then next century it will be T + D, in the next one T + 2 D,
and so on.
Scientists believe that there will be a world disaster if the average temperature is strictly greater than M degrees. Find out the numbers of centuries
till world disaster.

21

Input
The input contains three integer numbers each on a separate line: T, D, M
(1 T, D 100, 1 M 1000).

Output
Print the numbers of centuries before the global disaster.

Examples
20
1
23
100
10
90
1
1
1000

Input

Output

0
1000

Note
In the rst sample, the temperature increases by 1 each century. So, after 4
centuries it will be 24, which is more than 23, and a disaster will happen.
In the second sample, the disaster should happen in the current century,
so the answer is 0.

Solutions
This task describes the process of temperature increasing over the centuries,
so it is natural to use a cycle-based approach. Let's do a cycle that increases
the temperature by D until it is greater than M . We are interested in the
number of steps that this cycle will take, so we create a variable, set it equal
to zero, and increase it by one at each step of the cycle. When the cycle
nishes, this variable will be equal to the number of centuries passed until
disaster. Look at the code examples to see how to implement this solution.
22

It's also possible to solve this more simply, with a formula that immediately
gives the answer.

Python 2
t = i n t ( raw_input ( ) )
d = i n t ( raw_input ( ) )
m = i n t ( raw_input ( ) )
ans = 0
w h i l e t <= m:
ans += 1
t += d
p r i n t ans

Python 3
t = i nt ( input () )
d = i n t ( input () )
m = i nt ( input () )
ans = 0
w h i l e t <= m:
ans += 1
t += d
p r i n t ( ans )

Java
import j a v a . u t i l . Scanner ;
public class Solution {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new S o l u t i o n ( ) . run ( ) ;
}

p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
i nt t = input . nextInt () ;
i nt d = input . nextInt () ;
i nt m = input . nextInt () ;
i n t answer = 0 ;
w h i l e ( t <= m) {
t += d ;
answer++;
}
System . out . p r i n t l n ( answer ) ;
}

23

C++
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
i n t t , d , m;
c i n >> t >> d >> m;
i n t ans = 0 ;
w h i l e ( t <= m) {
ans++;
t += d ;
}
c o u t << ans << e n d l ;
return 0;
}

JavaScript
var t = p a r s e I n t ( r e a d l i n e ( ) )
var d = p a r s e I n t ( r e a d l i n e ( ) )
var m = p a r s e I n t ( r e a d l i n e ( ) )
var ans = 0
while ( t <= m) {
t += d
ans += 1

}
p r i n t ( ans )

PHP
<?php
$ t = i n t v a l ( f g e t s (STDIN) ) ;
$d = i n t v a l ( f g e t s (STDIN) ) ;
$m = i n t v a l ( f g e t s (STDIN) ) ;
$ans = 0 ;
w h i l e ( $ t <= $m) {
$ans++;
$ t += $d ;
}
p r i n t $ans ;
?>

24

2.3

Rating

Problem statement
School league tables, comparing the academic achievements of dierent schools,
are a common thing in many countries. Let's imagine that we have come up
with a new rating system for schools. The rating is computed once a year
and should reect the results of the school during some previous period of
time. Let's assume that from public sources we know the results of each
school. To compute the rating of a school in the current year, we will take
the results of this school over the last three years, and sum them up with the
following coecients:
Rating in the current year = 3 (result in the current year) + 2 (result in
the previous year) + (result two years before).
Before introducing this rating, we are going to implement it and to see how
it works for our school. Fortunately, we have a list of integers A1 , A2 , . . . AN ,
which denote the results of our school over the last N years (A1 corresponds
to the year when the result was recorded for the rst time ever, while AN
corresponds to the last year).
Now our goal is to try the new rating on this data and to see how the
rating has been changing over the past years. Note that our formula cannot
be directly applied to calculate the rating in the rst two years (years 1 and
2), because for this years there is not enough information. So, mathematically
we can rewrite our formula to compute Ri  the rating in year i:

if i is 1
3 A1 ,
Ri = 3 A2 + 2 A1 ,
if i is 2

3 Ai + 2 Ai1 + Ai2 , if i > 2


Your task is to implement this formula and print how the rating had been
changing over the last N years.

Input
The rst line contains integer N  the number of records (1 N 100).
The second line contains N space-separated integers A1 , A2 , . . . AN (1 Ai
100).
25

Output
Print N integers R1 , R2 , . . . RN  the rating values over the last N years.
Separate the numbers with spaces or line breaks.

Examples
3
8 3 6
5
4 1 2 1 5

Input

24 25 32

Output

12 11 12 8 19

Solutions
As long as the records are given as a list, it is natural to read them into a
list or an array in your program. Once that is done, we need to iterate over
all the years and to apply the rating formula given in the statement.

Python 2
n = i n t ( raw_input ( ) )
a = [ i n t ( token ) f o r token i n raw_input ( ) . s p l i t ( ) ]
f o r i in range (n) :
r = 3 a[ i ]
i f i > 0:
r += 2 a [ i 1 ]
i f i > 1:
r += a [ i 2 ]
print r ,

Python 3
n = i n t ( input () )
a = [ i n t ( token ) f o r token i n i n p u t ( ) . s p l i t ( ) ]
f o r i in range (n) :
r = 3 a[ i ]
i f i > 0:
r += 2 a [ i 1 ]
i f i > 1:
r += a [ i 2 ]
p r i n t ( r , end = ' ' )

26

Java
import j a v a . u t i l . Scanner ;
public class Solution {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new S o l u t i o n ( ) . run ( ) ;
}

p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
i nt n = input . nextInt () ;
i n t [ ] a = new i n t [ n ] ;
f o r ( i n t i = 0 ; i < n ; i ++) {
a [ i ] = input . nextInt () ;
}
f o r ( i n t i = 0 ; i < n ; i ++) {
int r = 3 a [ i ] ;
i f ( i > 0 ) r += 2 a [ i 1 ] ;
i f ( i > 1 ) r += a [ i 2 ] ;
System . out . p r i n t ( r + " " ) ;
}
System . out . p r i n t l n ( ) ;
}

C++
#i n c l u d e <i o s t r e a m >
#i n c l u d e <v e c t o r >
u s i n g namespace s t d ;
i n t main ( ) {
int n;
c i n >> n ;
v e c t o r <i n t > a ( n ) ;
f o r ( i n t i = 0 ; i < n ; i ++) c i n >> a [ i ] ;
f o r ( i n t i = 0 ; i < n ; i ++) {
int r = a [ i ] 3;
i f ( i > 0) {
r += a [ i 1 ] 2 ;
}
i f ( i > 1) {
r += a [ i 2 ] ;
}
c o u t << r << " " ;
}
return 0;
}

27

JavaScript
var
var
var
for

n = parseInt ( readline () )
line = readline () . s p l i t (' ')
a = []
( var i = 0 ; i < n ; i ++) {
a [ i ] = parseInt ( line [ i ] )

}
r e s u l t = ''
for ( var i = 0 ; i < n ; i ++) {
var r = a [ i ] 3
i f ( i > 0 ) r += a [ i 1 ] 2
i f ( i > 1 ) r += a [ i 2 ]
r e s u l t += t o S t r i n g ( r ) + ' '
}
print ( result )

PHP
<?php
$n = i n t v a l ( f g e t s (STDIN) ) ;
$ l i n e = f g e t s (STDIN) ;
$inputValues = s p l i t ( " " , $ l i n e ) ;
$a = a r r a y ( ) ;
f o r ( $ i = 0 ; $ i < $n ; $ i ++) {
$a [ $ i ] = i n t v a l ( $ i n p u t V a l u e s [ $ i ] ) ;
$ r = $a [ $ i ] 3 ;
i f ( $ i > 0) {
$ r += $a [ $ i 1 ] 2 ;
}
i f ( $ i > 1) {
$ r += $a [ $ i 2 ] ;
}
p r i n t $r . " " ;
}
?>

28

2.4

Almost Palindromes

Problem statement
A word is a palindrome if it is read the same forward and backward. Here
are a few examples: eye, civic, racecar. Let's say that a word is an
almost palindrome if it is possible to remove one of it's letters so that the
word becomes a palindrome. Your task is to write a program that for a given
word determines which letter to remove to get a palindrome.

Input
Input consists of a word of uppercase letters from 2 to 1000 characters long.

Output
Output the 1-indexed position (leftmost letter has position 1, the next one
has position 2 and so on) of the letter which should be removed. If there are
possible choices that lead to the palindrome, output any of those positions.
Note that you are required to remove a letter even if the given word is
already a palindrome (see example 3). If the given word is not an almost
palindrome, output -1.

Examples
RACEDCAR
BOBS
EE
ABC

Input

5
4
2
-1

Output

Solutions
It seems that the only approach we have is to try deleting each character
from the string and checking whether we get a palindrome. As we are going
to do many palindrome-checks, it seems sensible to dene a function that
takes a string and tells us if it is a palindrome or not.

29

How can we write such a function? To check if the given string is a


palindrome, we can verify that the rst character is the same as the last one,
the second character is equal to the last but one, and so on.
What about after that? Let's try to delete each character from the input
string and use our function to check if the remaining string is a palindrome.
A standard function that takes a substring from a string comes in handy.
You can see this in the examples from dierent languages given below.

Python 2
def is_pal ( s ) :
f o r i in range ( len ( s ) ) :
i f s [ i ] != s [ l e n ( s ) i 1 ] :
return False
r e t u r n True
s = raw_input ( )
ans = 1
f o r i in range ( len ( s ) ) :
i f i s _ p a l ( s [ : i ]+ s [ i + 1 : ] ) :
ans = i + 1
p r i n t ans

Python 3
def is_pal ( s ) :
f o r i in range ( len ( s ) ) :
i f s [ i ] != s [ l e n ( s ) i 1 ] :
return False
r e t u r n True
s = input ()
ans = 1
f o r i in range ( len ( s ) ) :
i f i s _ p a l ( s [ : i ]+ s [ i + 1 : ] ) :
ans = i + 1
p r i n t ( ans )

Java
import j a v a . u t i l . Scanner ;
public class Solution {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new S o l u t i o n ( ) . run ( ) ;
}
private boolean isPalindrome ( String s ) {
f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; i ++) {
i f ( s . charAt ( i ) != s . charAt ( s . l e n g t h ( ) i 1 ) ) {

30

return f a l s e ;

}
return true ;

p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
S t r i n g s = i n p u t . next ( ) ;
f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; ++i ) {
String z = s . s u b s t r i n g (0 , i ) + s . s u b s t r i n g ( i + 1) ;
i f ( isPalindrome ( z ) ) {
System . out . p r i n t l n ( i + 1 ) ;
return ;
}
}

System . out . p r i n t l n ( 1) ;

C++
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
bool isPalindrome ( s t r i n g s ) {
f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; i ++) {
i f ( s [ i ] != s [ s . l e n g t h ( ) i 1 ] ) r e t u r n f a l s e ;
}
return true ;
}
i n t main ( ) {
string s ;
c i n >> s ;
i n t ans = 1;
f o r ( i n t i = 0 ; i < s . l e n g t h ( ) ; i ++) {
i f ( isPalindrome ( s . substr (0 , i ) + s . substr ( i + 1) ) ) {
ans = i + 1 ;
}
}
c o u t << ans << e n d l ;
return 0;
}

JavaScript
function i s P a l i n d r o m e ( s ) {
for ( var i = 0 ; i < s . l e n g t h ; i ++) {
i f ( s [ i ] != s [ s . l e n g t h i 1 ] ) {

31

}
}

return f a l s e ;

return true ;

var s = r e a d l i n e ( ) ;
var ans = 1;
for ( var i = 0 ; i < s . l e n g t h ; i ++) {
i f ( isPalindrome ( s . s u b s t r i n g (0 , i ) + s . s u b s t r i n g ( i + 1) ) ) {
}

ans = i + 1 ;

}
p r i n t ( ans )

PHP
<?php
f u n c t i o n isPalindrome ( $s ) {
f o r ( $ i = 0 ; $ i < s t r l e n ( $ s ) ; $ i ++) {
i f ( $ s [ $ i ] != $ s [ s t r l e n ( $ s ) $ i 1 ] ) r e t u r n f a l s e ;
}
return true ;
}

?>

$ s = t r i m ( f g e t s (STDIN) ) ;
$ans = 1;
f o r ( $ i = 0 ; $ i < s t r l e n ( $ s ) ; $ i ++) {
i f ( i s P a l i n d r o m e ( s u b s t r ( $s , 0 , $ i ) . s u b s t r ( $s , $ i + 1 ) ) ) {
$ans = $ i + 1 ;
}
}
p r i n t $ans ;

2.5

Drawing Christmas Tree

Problem statement
Let us dene the ladder of order n as a sequence of n lines numbered from 1
to n such that line i consists of (2 i 1) '*' characters. For example: ladder
of order 1 is just a line with a single character '*' in it. Ladder of order 2 is

*
***
Ladder of order 3 is
32

*
***
*****
And so on.
Let us dene the Christmas Tree of order 1 as a ladder of order 1.
Let us dene the Christmas Tree of order n (n > 1) as a Christmas Tree
of order n 1 followed by the ladder of order n.
Given n, print Christmas Tree of order n.

Input
The rst line contains a single integer: n (1 n 100).

Output
Print Christmas Tree of order n. Note that you have to insert leading

spaces into all lines except the last one in order to align the tree
properly. Do not print any trailing spaces.

33

Examples
Input Output
1
*
*
2
*
***
*
*
***
*
***
*****
*
5
***
*****
*******
*
***
*****
*******
*********

Solutions
In this task we are asked to print a ladder of order 1, then a ladder of order
2, . . . , and nally a ladder of order n. Let's make a loop that iterates the
value of k from 1 to n, and print a ladder of order k inside this loop:

for k = 1..n
print a ladder of order k
To print a ladder of order k , we have to print k lines. For each line we
know the number of `*'s in it (line number i has 2i 1 `*'s). We also need to
nd out how many spaces to print at the beginning of the line. By considering
several examples, we gure out that if we are printing line number i (among
the lines within the same ladder), we have to print exactly (n i) leading
spaces.

34

Python 2
n = i n t ( raw_input ( ) )
f o r k i n r a n g e ( 1 , n+1) :
f o r i i n r a n g e ( 1 , k+1) :
line = ' '
f o r j in range (n i ) :
l i n e += ' '
f o r j i n r a n g e ( 2 i 1) :
l i n e += ' '
print line

Python 3
n = i n t ( input () )
f o r k i n r a n g e ( 1 , n+1) :
f o r i i n r a n g e ( 1 , k+1) :
line = ' '
f o r j in range (n i ) :
l i n e += ' '
f o r j i n r a n g e ( 2 i 1) :
l i n e += ' '
print ( line )

Java
import j a v a . u t i l . Scanner ;
public class Solution {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new S o l u t i o n ( ) . run ( ) ;
}

p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
i nt n = input . nextInt () ;
f o r ( i n t k = 1 ; k <= n ; k++) {
f o r ( i n t i = 1 ; i <= k ; i ++) {
f o r ( i n t j = 0 ; j < n i ; j ++) {
System . out . p r i n t ( " " ) ;
}
f o r ( i n t j = 0 ; j < 2 i 1 ; j ++) {
System . out . p r i n t ( " " ) ;
}
System . out . p r i n t l n ( ) ;
}
}
}

35

C++
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
int n;
c i n >> n ;
f o r ( i n t k = 1 ; k <= n ; k++) {
f o r ( i n t i = 1 ; i <= k ;
for ( int j = 0;
c o u t <<
}
for ( int j = 0;
c o u t <<
}
c o u t << e n d l ;
}
}
return 0;
}

i ++) {
j < n i ; j ++) {
" ";
j < 2 i 1 ; j ++) {
"" ;

JavaScript
var n = p a r s e I n t ( r e a d l i n e ( ) ) ;
for ( var k = 1 ; k <= n ; k++) {
for ( var i = 1 ; i <= k ; i ++) {
var l i n e = "" ;
for ( var j = 0 ; j < n i ; j ++) {
}

l i n e += ' ' ;

for ( var j = 0 ; j < 2 i 1 ; j ++) {

l i n e += '* ' ;
}
print ( line ) ;

PHP
<?php
$n = i n t v a l ( f g e t s (STDIN) ) ;
f o r ( $k = 1 ; $k <= $n ; $k++) {
f o r ( $ i = 1 ; $ i <= $k ; $ i ++) {
$ l i n e = "" ;
f o r ( $ j = 0 ; $ j < $n $ i ; $ j++) {
$ l i n e .= " " ;
}
f o r ( $ j = 0 ; $ j < 2 $ i 1 ; $ j++) {
$ l i n e .= " " ;
}
p r i n t $ l i n e . "\n" ;
}
}
?>

36

2.6

Text Decompression

Problem statement
You might know that compression of data is used to reduce the amount of
storage needed for the documents or to reduce the amount of transmitting
data.
One simple way to compress the string is to use repetitions in it. Let's
look at the string ABABABAB. One can see that this string is in fact string
AB repeated four times. So let's rewrite it as (4AB). The same thing can be
applied not only to the whole string, but to it's substrings (parts) as well.
So, having a string AACACACB, we can notice that the substring ACACAC has
a repetition and rewrite the whole string as A(3AC)B.
Let's turn to more complicated examples. What if we have a string
ABABABCABABABC. It is in fact a string ABABABC repeated two times, so we
can rewrite it as (2ABABABC). Than we notice that substring ABABAB also has
repetition, so we compress it too and nally get (2(3AB)C).
Let's describe the compression formally. We have a string of Latin letters.
In each compression step, we can take a substring and rewrite it. We can
take any substring that is composed only from letters and has repetition.
You are given the result of the compression. Find the decompressed
string.

Input
A string of uppercase Latin letters. The length of the string is between 1
and 10000.

Output
Print the decompressed string. Result is guaranteed to have not more that
105 characters.

37

Examples
(2(3AB)C)
ABC
(10A)
(3A)B(3C)D
(2A)

Input

ABABABCABABABC
ABC
AAAAAAAAAA
AAABCCCD
AA

Output

Solutions
To solve this problem, we can use a thing known as recursive descent parsing.
In this case, imagine that we are scanning the given string, moving a pointer
along it. If in the current position we observe a letter, we can simply add it
to the result. But if we observe an opening bracket, we have to

extract the number of repetitions  let's say it is K;


obtain a nested part;
add K copies of the inner part to the result.
After that, we can continue parsing the string until we get to the end.
Consider that when we get a nested part, we can use essentially the same
procedure that we use for the whole string. That means that we can make a
recursive call of our procedure on a smaller (nested) part of itself. The only
dierence is that the procedure must be aware of the fact that it is called
for a nested part, and somehow know where to stop parsing. So we can add
one more stopping condition: it we observe a closing bracket, that means we
should stop and return the result. We illustrate this idea with the following
code examples.
Note: don't panic if you don't get the solution here. The problem is tricky,
and coding it requires relatively strong knowledge of both a programming
language and an understanding of recursion. These delights await you in
your university Computer Science course which you are no doubt going to
do!

38

Python 2
d e f decompress ( ) :
result = ' '
global s
g l o b a l pos
w h i l e pos < l e n ( s ) and s [ pos ] != ' ) ' :
i f s [ pos ] == ' ( ' :
pos += 1
rep = 0
w h i l e s [ pos ] . i s d i g i t ( ) :
r e p = r e p 10 + i n t ( s [ pos ] )
pos += 1
i n n e r = decompress ( )
f o r i in range ( rep ) :
r e s u l t += i n n e r
pos += 1
else :
r e s u l t += s [ pos ]
pos += 1
return r e s u l t
s = raw_input ( )
pos = 0
p r i n t decompress ( )

Python 3
d e f decompress ( ) :
result = ' '
global s
g l o b a l pos
w h i l e pos < l e n ( s ) and s [ pos ] != ' ) ' :
i f s [ pos ] == ' ( ' :
pos += 1
rep = 0
w h i l e s [ pos ] . i s d i g i t ( ) :
r e p = r e p 10 + i n t ( s [ pos ] )
pos += 1
i n n e r = decompress ( )
f o r i in range ( rep ) :
r e s u l t += i n n e r
pos += 1
else :
r e s u l t += s [ pos ]
pos += 1
return r e s u l t
s = input ()
pos = 0
p r i n t ( decompress ( ) )

39

Java
import j a v a . u t i l . Scanner ;
public class Solution {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
new S o l u t i o n ( ) . run ( ) ;
}
private String s ;
p r i v a t e i n t pos ;
S t r i n g decompress ( ) {
S t r i n g B u i l d e r r e s u l t = new S t r i n g B u i l d e r ( ) ;
w h i l e ( pos < s . l e n g t h ( ) && s . charAt ( pos ) != ' ) ' ) {
i f ( s . charAt ( pos ) == ' ( ' ) {
pos++;
i n t rep = 0 ;
w h i l e ( C h a r a c t e r . i s D i g i t ( s . charAt ( pos ) ) ) {
r e p = r e p 10 + s . charAt ( pos ) ' 0 ' ;
pos++;
}
S t r i n g i n n e r = decompress ( ) ;
f o r ( i n t i = 0 ; i < r e p ; i ++) {
r e s u l t . append ( i n n e r ) ;
}
pos++;
} else {
r e s u l t . append ( s . charAt ( pos ) ) ;
pos++;
}
}
return r e s u l t . toString () ;
}

p r i v a t e v o i d run ( ) {
Scanner i n p u t = new Scanner ( System . i n ) ;
s = i n p u t . next ( ) ;
pos = 0 ;
System . out . p r i n t l n ( decompress ( ) ) ;
}

C++
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
string s ;
i n t pos ;
s t r i n g decompress ( ) {
s t r i n g r e s u l t = "" ;
w h i l e ( pos < s . l e n g t h ( ) && s [ pos ] != ' ) ' ) {

40

i f ( s [ pos ] == ' ( ' ) {


pos++;
i n t rep = 0 ;
w h i l e ( i s d i g i t ( s [ pos ] ) ) {
r e p = r e p 10 + s [ pos ] ' 0 ' ;
pos++;
}
s t r i n g i n n e r = decompress ( ) ;
f o r ( i n t i = 0 ; i < r e p ; i ++) {
r e s u l t += i n n e r ;
}
pos++;
} else {
r e s u l t += s [ pos ] ;
pos++;
}

}
return r e s u l t ;

i n t main ( ) {
c i n >> s ;
pos = 0 ;
c o u t << decompress ( ) << e n d l ;
return 0;
}

JavaScript
function decompress ( ) {
var r e s u l t = "" ;
while ( pos < s . l e n g t h && s [ pos ] != ') ' ) {
i f ( s [ pos ] == '( ' ) {
pos++;

var r e p = 0 ;
while ( s [ pos ] >= '0 ' && s [ pos ] <= '9 ' ) {
}

r e p = r e p 10 + p a r s e I n t ( s [ pos ] ) ;
pos++;

var i n n e r = decompress ( ) ;
for ( var i = 0 ; i < r e p ; i ++) {

}
}

r e s u l t += i n n e r ;
}
pos++;
} else {
r e s u l t += s [ pos ] ;
pos++;
}

return r e s u l t ;

var s = r e a d l i n e ( ) ;
var pos = 0 ;

p r i n t ( decompress ( ) ) ;

41

PHP
<?php
f u n c t i o n decompress ( ) {
g l o b a l $s ;
g l o b a l $pos ;
$ r e s u l t = "" ;
w h i l e ( $pos < s t r l e n ( $ s ) && $ s [ $pos ] != ' ) ' ) {
i f ( $ s [ $pos ] == ' ( ' ) {
$pos++;
$rep = 0 ;
w h i l e ( is_numeric ( $ s [ $pos ] ) ) {
$ r e p = $ r e p 10 + i n t v a l ( $ s [ $pos ] ) ;
$pos++;
}
$ i n n e r = decompress ( ) ;
f o r ( $ i = 0 ; $ i < $ r e p ; $ i ++) {
$ r e s u l t .= $ i n n e r ;
}
$pos++;
} else {
$ r e s u l t .= $ s [ $pos ] ;
$pos++;
}
}
return $result ;
}
$ s = t r i m ( f g e t s (STDIN) ) ;
$pos = 0 ;
p r i n t decompress ( ) ;
?>

42

Вам также может понравиться