sashomaga

Sand-glass

Dec 24th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2. //Input
  3. //The input data should be read from the console.
  4. //You have an integer number N (always odd number) showing the height of the sand clock.
  5. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  6. //Output
  7. //The output should be printed on the console.
  8. //You should print the hourglass on the console. Each row can contain only the following characters: “.” (dot) and “*” (asterisk).
  9. //As shown in the example: the middle row must contain only one ‘*’ and all other symbols must be “.”.
  10. //Every next row (up or down from the middle one) must contain the same number of ‘*’ as the previous one plus two. You should only use “.” to
  11. //fill-in the rows, where necessary.
  12.  
  13. class Program
  14. {
  15.     static void Main()
  16.     {
  17.         int height = int.Parse(Console.ReadLine());
  18.        
  19.         bool direction = true;
  20.         for (int i = 0, reverse = height - 1; i < height; i++, reverse--) //liness
  21.         {
  22.             for (int j = 1; j <= height; j++) //rows
  23.             {
  24.                 if (i > height / 2)
  25.                 {
  26.                     direction = false;
  27.                 }
  28.                 if (direction == true)
  29.                 {
  30.                     if (i >= j)
  31.                     {
  32.                         Console.Write(".");
  33.                     }
  34.                     else if (j + i > height)
  35.                     {
  36.                         Console.Write(".");
  37.                     }
  38.                     else
  39.                     {
  40.                         Console.Write("*");
  41.                     }
  42.                 }
  43.                 if (direction == false)
  44.                 {
  45.                     if (reverse >= j)
  46.                     {
  47.                         Console.Write(".");
  48.                     }
  49.                     else if (reverse + j > height)
  50.                     {
  51.                         Console.Write(".");
  52.                     }
  53.                     else
  54.                     {
  55.                         Console.Write("*");
  56.                     }
  57.                 }
  58.             }
  59.             Console.WriteLine();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment