Problem: Butterfly

Write a program that takes an integer n from the console and draws a butterfly 2 * n - 1 columns wide and 2 * (n - 2) + 1 rows tall, as in the examples below. The left and right parts are n - 1 wide.

Input Data

The input is an integer n within the range [3 … 1000].

Output Data

Print on the console 2 * (n - 2) + 1 text rows, depicting the butterfly as in the examples.

Sample Input and Output

Input Output Input Output
3 *\ /*
  @  
*/ \*
5 ***\ /***
---\ /---
***\ /***
    @    
***/ \***
---/ \---
***/ \***
Input Output
7 *****\ /*****
-----\ /-----
*****\ /*****
-----\ /-----
*****\ /*****
      @      
*****/ \*****
-----/ \-----
*****/ \*****
-----/ \-----
*****/ \*****

Hints and Guidelines

We can see in the explanation that the input data will be taken from only one row which contains an integer within the range [3 … 1000]. This is why we will use a variable of int type.

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 – beginning *, middle part \ / and 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 *, a middle part \ / and an end *, and on an odd row – beginning -, middle part \ / and an end -. This is why we must add an if-else condition to check if the row is even or odd and then to draw one of the two types of rows.

Printing the Body and the Lower Wing

In order to create the body of the butterfly we can use the variable halfRowSize again and print on the console exactly one row. The body structure begins with (white space), middle @ and ends with (white space).

Now we need to print the lower wing, which is the same as the upper one.

Testing in the Judge System

Test your solution here: https://judge.softuni.org/Contests/Practice/Index/513#1.

results matching ""

    No results matching ""