In the previous chapter we looked at nested loops and how to use them to draw different kinds of figures on the console. We learned how to print figures with different sizes, thinking of an appropriate logic to construct them using single and nested for
loops in combination with various calculations and program logic:
for (int row = 1; row <= 5; row++) {
System.out.print("*");
for (int col = 1; col < 5; col++) {
System.out.print(" *");
}
System.out.println();
}
We also declared our own method repeatStr(…)
which helps us to print a given symbol (or a series of characters) certain number of times:
Let's solve several exam problems related to nested loops to practice what we have learned and to further develop our algorithmic thinking.
Write a program that reads from the console an integer n and draws a fortress with width 2 * n columns and height n rows like the examples below. The left and right columns on the inside are with width n / 2.
The input is an integer n in the range of [3 … 1000].
Print on the console n text rows, depicting the fortress, exactly like the examples.
Input | Output | Input | Output |
---|---|---|---|
3 | /^\/^\ | | \_/\_/ |
4 | /^^\/^^\ | | | | \__/\__/ |
Input | Output | Input | Output |
---|---|---|---|
5 | /^^\__/^^\ | | | | | __ | \__/ \__/ |
8 | /^^^^\____/^^^^\ | | | | | | | | | | | ____ | \____/ \____/ |
We can see from the task that the input data will be only one line which will contains an integer within the range [3 … 1000]. Therefore, we will use a variable of type int
.
After we have declared and initialized the input data, we must divide the fort into three parts:
- roof
- body
- base
We can see from the examples that the roof is made of two towers and a middle part. Each tower has a beginning /
, middle part ^
and an end \
.
The size of the middle part is n / 2
, therefore we can write this value in a new variable. It will store the size of the middle part of the tower.
Now we declare a second variable, in which we will store the value of the part between the two towers. The middle part of the roof has size of 2 * n - 2 * colSize - 4
.
In order to print on the console the roof, we will use our method repeatStr(…)
, which accepts two parameters (string, int)
and concatenate a certain symbol (or series of characters) n times.
The body of the fort contains a beginning |
, middle part (white space)
and an end |
. The middle part is a blank space with size of 2 * n - 2
. The number of the rows used for walls could be found by the given parameters - n - 3
.
In order to draw the last row, which is a part of the base, we need to print the beginning |
, the middle part (white space)_(white space)
and an end |
. In order to do this, we can use the already declared variables colSize
and midSize
, because we can see from the examples that they are equal to the _
in the roof.
We add to the values of the blank spaces + 1
, because we have in the examples one blank space more.
The structure of the base of the fort is the same as the one of the roof. It is made of two towers and a middle part. Each tower has a beginning \
, middle part _
and an end /
.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/658#0.
Write a program that reads from the console an integer n and draws a butterfly with width 2 * n - 1 columns and a height 2 * (n - 2) + 1 rows like the examples below. The left and right parts are with width n - 1.
The input is an integer n in the range of [3 … 1000].
Print on the console 2 * (n - 2) + 1 text rows, depicting the butterfly, exactly like the examples.
Input | Output | Input | Output |
---|---|---|---|
3 | *\ /* @ */ \* |
5 | ***\ /*** ---\ /--- ***\ /*** @ ***/ \*** ---/ \--- ***/ \*** |
Input | Output |
---|---|
7 | *****\ /***** -----\ /----- *****\ /***** -----\ /----- *****\ /***** @ *****/ \***** -----/ \----- *****/ \***** -----/ \----- *****/ \***** |
From the task we can see that the input data will be only one line which will contains an integer within the range [3 … 1000]. That is why we will use a variable of type int
.
We can divide the figure into 3 parts - upper wing, body and lower wing. In order to draw the upper wing, we need to divide it into parts - a beginning *
, middle part \ /
and an end *
. After looking at the examples we find out that the beginning is with size n - 2
.
We can also see that the upper wing is with size n - 2
, and that's why we can make a loop which repeats halfRowSize
times.
We can see in the examples that on an even row we have a beginning -
, middle part \ /
and an end *
, and on a odd - a beginning *
, middle part \ /
and an end -
. Therefore, we have to do an if-else
condition to check if the row is even or odd and then to draw one of the two types of rows.
In order to create the body of the butterfly, we can use the variable halfRowSize
and to print on the console exactly one line. The structure of the body begins with (white spave)
, middle part @
and ends with (white space)
.
Now we need to print the lower wing, which is the same as the upper one.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/658#1.
Write a program that reads from the console an integer n and draws the warning sign STOP with size as in the example below.
The input is an integer N in the range of [3 … 1000].
Print on the console text lines which depict the warning sign STOP as in the examples below.
Input | Output | Input | Output |
---|---|---|---|
3 | ...._______.... ...//_____\\... ..//_______\\.. .//_________\\. //___STOP!___\\ \\___________// .\\_________//. ..\\_______//.. |
6 | ......._____________....... ......//___________\\...... .....//_____________\\..... ....//_______________\\.... ...//_________________\\... ..//___________________\\.. .//_____________________\\. //_________STOP!_________\\ \\_______________________// .\\_____________________//. ..\\___________________//.. ...\\_________________//... ....\\_______________//.... .....\\_____________//..... |
Input | Output |
---|---|
7 | ........_______________........ .......//_____________\\....... ......//_______________\\...... .....//_________________\\..... ....//___________________\\.... ...//_____________________\\... ..//_______________________\\.. .//_________________________\\. //___________STOP!___________\\ \\___________________________// .\\_________________________//. ..\\_______________________//.. ...\\_____________________//... ....\\___________________//.... .....\\_________________//..... ......\\_______________//...... |
We can see from the task explanation that the input data will come from only one line which contains an integer in the range of [3 … 1000]. Therefore, we will use a variable of type int
.
We can divide the figure into 3 parts - upper, middle and lower. The upper part contains two sub-parts - first row and rows in which the sign widens. The first row is made of a beginning .
, middle part _
and an end .
. After looking at the examples we can say that the beginning is with size n + 1
so it is good to separate this value in a separate variable.
We must also create a second variable, in which we will store the value of the middle of the first row which has a size of 2 * n + 1
.
After we have declared and initialized the two variables, we can print the first row on the console. Let's not forget moving to a new line on the console with System.out.println(…)
.
In order to draw the rows in which the sign is getting "wider", we have to create a loop, to rotate n
times. The structure of a row contains a beginning .
, //
+ middle part _
+ \\
and an end .
. In order to reuse the already created variables, we have to decrease the dots
with 1 and the underscores
with 2, because we have already printed the first row, and the dots and underscores in the next rows of the figure are decreasing.
In each following iteration the beginning and the end are decreasing with 1, and the middle part increases by 2.
The middle part of the figure has a beginning //
+ _
, middle part STOP!
and an end _
+ \\
. The count of the underscores _
is (underscores - 5) / 2
.
The lower part of the figure where the sign decreases, can by done again by creating a loop, which rotates n
times. The structure of a single line is a beginning .
+ \\
, middle part _
and an end //
+ .
. The number of the dots in the first iteration should be 0 and in each following one it increases by one. Therefore, we can say that the size of the dots in the lower part of the figure is equals to i
.
In order for our program to work properly, in each iteration of the loop we need to decrease the number of _
with 2.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/658#2.
Write a program that has an input from the console an odd integer n and draws a vertical arrow with size as in the examples below.
The input is an integer odd number n in the range of [3 … 79].
Print a vertical arrow on the console, in which "#
" (sharp) marks the outline of the arrow, and the ".
" - the rest.
Input | Output | Input | Output |
---|---|---|---|
3 | .###. .#.#. ##.## .#.#. ..#.. |
5 | ..#####.. ..#...#.. ..#...#.. ..#...#.. ###...### .#.....#. ..#...#.. ...#.#... ....#.... |
Input | Output |
---|---|
9 | ....#########.... ....#.......#.... ....#.......#.... ....#.......#.... ....#.......#.... ....#.......#.... ....#.......#.... ....#.......#.... #####.......##### .#.............#. ..#...........#.. ...#.........#... ....#.......#.... .....#.....#..... ......#...#...... .......#.#....... ........#........ |
We can see from the task explanation that the input data will come from only one line which contains an integer in the range of [3 … 79]. Therefore, we will use a variable of type int
.
We can divide the figure into 3 parts - upper, middle and lower. The upper part consists of two subparts - starting row and body of the arrow. We can see from the examples that the number of the external dots in the starting row and in the body of the arrow are (n - 1) / 2
. We can write this value in a variable outerDots
.
The number of the internal dots in the body of the arrow is (n - 2)
. We need to create a variable with name innerDots
, which will store this value.
We can see from the examples the structure of the first row. We must use the declared and initialized variables outerDots
and n
, in order to print the starting row. We must not forget that we have to move to a new line with System.out.println();
.
In order to draw on the console the body of the arrow, we have to create a loop, which rotates n - 2
times.
The middle part of the figure consists of a beginning #
, middle part .
and an end #
. We see from the examples that the number of the #
is equals to outerDots
increased by 1 and that is why we can use again the same variable.
To draw the lower part of the arrow, we need to assign new values to the variables outerDots
and innerDots
.
The loop, we are going to make must rotate n - 2
times and we need to print the last row of the figure separately. On each iteration outerDots
increases by 1, and innerDots
decreases by 2.
The last row of our figure is made of a beginning .
, middle part #
and an end .
. The number of the .
is equals to outerDots
.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/658#3.
Write a program that takes as an input an integer n and draws an axe with size as shown below.
The width of the axe is 5 * N
columns.
The input is an integer n in the range of [2..42].
Print on the console an axe, exactly like the examples.
Input | Output | Input | Output |
---|---|---|---|
2 | ------**-- ------*-*- *******-*- ------***- |
5 | ---------------**-------- ---------------*-*------- ---------------*--*------ ---------------*---*----- ---------------*----*---- ****************----*---- ****************----*---- ---------------*----*---- --------------********--- |
Input | Output |
---|---|
8 | ------------------------**-------------- ------------------------*-*------------- ------------------------*--*------------ ------------------------*---*----------- ------------------------*----*---------- ------------------------*-----*--------- ------------------------*------*-------- ------------------------*-------*------- *************************-------*------- *************************-------*------- *************************-------*------- *************************-------*------- ------------------------*-------*------- -----------------------*---------*------ ----------------------*-----------*----- ---------------------***************---- |
In order to solve the problem, first we need to calculate the dashes on the left, the middle dashes, the dashes on the right and the whole length of the figure.
After we have declared and initialized the variables, we can begin drawing the figure by starting with the upper part. We can see from the examples what is the structure of the first row and to create a loop which rotates n
times. On each iteration the middle dashes increase by 1, and the dashes on the right decrease by 1.
In order to use the created variables when drawing the handle of the ax you need to decrease the middle dashes by 1, and the dashes on the right to increase by 1.
The handle of the axe we can draw by rotating the loop n - 2
times. We can see in the examples what its structure is.
The lower part of the figure we have to divide into 2 subparts - the head of the axe and the last row from the axe. The head of the axe we will print on the console as we create a loop which rotates n / 2 - 1
times. On each iteration the dashes on the left and the dashes on the right decrease by 1, and the middle dashes increase by 2.
For the last row of the figure we can use the three declared and initialized variables leftDashes
, middleDashes
, rightDashes
.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/658#4.