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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main() { int N = int.Parse(Console.ReadLine()); int EntireWidth = N * 2 + 1; int Height = 6 + ((N - 3) / 2) * 3; int SailsHeight = (Height / 3) * 2; int baseOfBoat = Height / 3; int newN = N; //Building the first part: for (int z = 0; z < SailsHeight-1; z++) { Console.Write(new string('.', newN)); if (z != 0) { Console.Write(new string('*', 1)); } if (z > 1) { Console.Write(new string('.', z - 1)); } Console.Write(new string('*', 1)); if (z > 1) { Console.Write(new string('.', z - 1)); } if (z != 0) { Console.Write(new string('*', 1)); } Console.Write(new string('.', newN)); newN--; Console.WriteLine(); } //Building the 2nd part: newN = N; Console.WriteLine(new string('*',EntireWidth)); //Building the 3rd part: for (int i = 0; i < baseOfBoat-1; i++) { Console.Write(new string('.', i+1)); Console.Write(new string('*', 1)); Console.Write(new string('.',(EntireWidth - 3 - (2 * i + 1))/2)); Console.Write(new string('*', 1)); Console.Write(new string('.',(EntireWidth - 3 - (2 * i + 1))/2)); Console.Write(new string('*', 1)); Console.Write(new string('.', i + 1)); Console.WriteLine(); } newN = N; //Building the bottom line: int LastCalculation = (EntireWidth - newN)/2; Console.Write(new string('.', LastCalculation)); Console.Write(new string('*', newN)); Console.Write(new string('.', LastCalculation)); } } |
If by any chance, you would like to test the program, you may do it from here.