The last (sixth) problem of the "Programming Basics" Practical Exam requires the using of several nested loops and more complex logic in them. The task examines students' ability to think algorithmically and to solve non-trivial problems that require loops. HLet's take a few examples of exam problems.
For a pair of numbers a and b generate all fours n1, n2, n3, n4, for which a ≤ n1 <n2 <n3 <n4 ≤ b.
The input contains two integers a and b in the range [0 … 1000], one per line.
The output contains all quadruple searches, in ascending order, one per line.
|3 4 5 6
3 4 5 7
3 4 6 7
3 5 6 7
4 5 6 7
|15 16 17 18
15 16 17 19
15 16 17 20
15 16 18 19
15 16 18 20
15 16 19 20
15 17 18 19
15 17 18 20
15 17 19 20
15 18 19 20
16 17 18 19
16 17 18 20
16 17 19 20
16 18 19 20
17 18 19 20
|10 11 12 13|
We will read the input data from the console. We also create the additional variable
count, which will keep track of existing number ranges.
We will most easily solve the problem if we logically divide it in parts. If we are required to draw all the rows from a number between
b, we will do it with one loop that takes all the numbers from
b. Let's think how we make this with two-number rows. The answer is easy - we will use nested loops.
We can test the uncomplete program to see if it's true so far. It must print all pairs of numbers
j for which
i ≤ j.
Since each next number of the row must be greater than the previous one, the second loop will revolve around
i + 1 (the next greater number). Accordingly, if there is no sequence of two incremental numbers (
b are equal), the second loop will not be fulfilled and nothing will be printed on the console.
Similarly, it remains to implement the nested loops for four numbers. We will add an increase of the counter that we initialized in order to know if there is such a sequence.
Finally, we will check if the counter is equal to 0 and we will print "No" on the console accordingly, if so.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/516#10.
By a given number n and a minimum area m, all rectangles with whole coordinates in the range [-n…n] with an area of at least m are generated. Generated rectangles must be print in the following format:
(left, top) (right, bottom) -> area
Rectangles are set using the top left and bottom right corner. The following inequalities are in effect:
- -n ≤ left < right ≤ n
- -n ≤ top < bottom ≤ n
Two numbers, one per line, are entered from the console:
- An integer n in the range [1 … 100] - sets the minimum and maximum coordinates of a peak.
- An integer m in the range [0 … 50 000] - sets the minimum area of the generated rectangles.
- The described rectangles should be printed on the console in a format such as in the examples below.
- If there are no rectangles for the n and m, then "No" is displayed.
- The order of rectangles is irrelevant.
|(-1, -1) (0, 1) -> 2
(-1, -1) (1, 0) -> 2
(-1, -1) (1, 1) -> 4
(-1, 0) (1, 1) -> 2
(0, -1) (1, 1) -> 2
|(-3, -3) (3, 3) -> 36|
Read the input data from the console. We will also create a counter, which will keep the number of rectangles found.
It is very important to be able to imagine the problem before we begin to solve it. In our case it is required to search for rectangles in a coordinate system. The thing we know is that the left point will always have the coordinate
x, smaller than the right. Accordingly, the upper one will always have a smaller
у coordinate than the lower one. To find all the rectangles, we'll have to make a loop similar to the previous problem, but this time, not every next loop will start from the next number because some of the coordinates can be equal (for example
With the variables
right we will follow the coordinates horizontally and with
bottom - vertically.
The important thing here is knowing which coordinates they are so we can correctly calculate the sides of the rectangle. Now we have to find the area of the rectangle and check if it is greater than or equal to
m. The one side will be the difference between
right and the other one - between
bottom. Since the coordinates may be eventually interchanged, we will use absolute values. Again, we add the counter in the loop, counting only the rectangles we write. It is important to note that the writing order is
bottom, as it is set in the problem's description.
Finally, we print "No" if there are no such rectangles.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/516#11.