C# – “Painting” with the console – Kaspichania Boats

Today I have decided to take a look at some new C# contest problems, for C# level 1 of the Telerik Academy. I thought that this would be rather easy for me, as far as I have successfully passed the C# level 2, but to my surprise I needed at least 30 minutes to come up with a working solution.

That is why I have decided, to post the solution I came up with, because after spending some 40 minutes on it I was proud of achieving 100%. The desciption of the problem can be downloaded here.

Pretty much, the task is the following – you read an integer number from the console and you should print a boat with a bottom size equal to this number. Or with other words, if you write 11, you should get this:

Boat

In order to come up with a solution, I have used one feature of C#, which is really helpful for such problems, the “new string()”. Pretty much, it helps you to write a specific symbol given numbers of time. Once, you are aware of this option, the only thing you should do is to find a way to calculate the times of appearances of the symbols “*” and “.”.

Once you are ready with this, you start using ConsoleWrite() and ConsoleWriteLine() in order to come up with a painting. In my solution, I have divided the building of the boat in 4 parts – first I build the top, then the base (which is the row full with stars on the picture), the bottom part and the last line. The code is not very complicated and understandable:

If by any chance, you would like to test the program, you may do it from here.

Tagged with: , , , ,