Advertisement
thegrudge

TheExplorer

Nov 21st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2.                    
  3. public class TheExplorer
  4. {
  5.     public static void Main()
  6.     {
  7.         // input n = diamond width
  8.         int n = int.Parse(Console.ReadLine());
  9.        
  10.         /*
  11.             In the loop below the last row is n-1 because the counting starts from 0;
  12.            
  13.             Bacause of the fact that the last row is n-1, then the central row will be n/2.
  14.             For example: n = 5;
  15.                 1) if we start counting from 1;
  16.             so five elements will be: 12345. And the Middle element is "3"
  17.             -> center will be n/2 + 1 or 5/2 + 1 -> 2+1 = 3;
  18.                 2) if we start counting from 0;
  19.             so five elements will be: 01234. As you can see mid elelemt is "2"
  20.             -> center will be n/2 or 5/2 = 2;  
  21.         */ 
  22.        
  23.         for (int i = 0; i < n; i++)
  24.         {
  25.             // check is the row counter(i) is first or last row.
  26.             if (i == 0 || i == n - 1)
  27.             {
  28.                 string outDiamond = new string('-', n / 2);
  29.                 string diamondLine = new string('*', 1);
  30.                 Console.WriteLine("{0}{1}{0}", outDiamond, diamondLine);
  31.             }
  32.             // draw the central line
  33.             else if (i == (n/2))
  34.             {
  35.                 string diamondLine = new string('*', 1);
  36.                 string innerDiamond = new string('-', n - 2);
  37.                 Console.WriteLine("{0}{1}{0}", diamondLine, innerDiamond);
  38.             }
  39.            
  40.             // Above the central line of the diamond
  41.             else if (i < (n/2))
  42.             {
  43.                 string outDiamond = new string('-', n / 2 - i);
  44.                 string diamondLine = new string('*', 1);
  45.                 string innerDiamond = new string('-', 2*(i-1)+1);
  46.                 Console.WriteLine("{0}{1}{2}{1}{0}", outDiamond, diamondLine, innerDiamond);
  47.             }
  48.             // Every other case is covered so  this is for the draw
  49.             // bellow the central line.
  50.             else
  51.             {
  52.                 string outDiamond = new string('-', Math.Abs(n/2-i));
  53.                 string diamondLine = new string('*', 1);
  54.                 string innerDiamond = new string('-', Math.Abs(n-2 - 2*(i - n/2)));
  55.                 Console.WriteLine("{0}{1}{2}{1}{0}", outDiamond, diamondLine, innerDiamond);
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement