Posts Tagged ‘solution’

        private static void Carpets()
        {
            // 4. Carpets
            int nWidth = int.Parse(Console.ReadLine());
            int nHalf = nWidth / 2;
            string lastChar = "";
            //string[] arrAllRows2Half = new string[nHalf-1];
            string[][] jaggedArray = new string[nHalf][];

            // rows & columns
            // I. rows - upper
            for (int row = 1; row <= nHalf; row++)
            {
                string[] arrChars2Half = new string[nWidth];
                // II cols
                for (int col2Half = 0; col2Half < nHalf; col2Half++)
                {
                    if (col2Half < nHalf - row)
                    {
                        arrChars2Half[col2Half] = ".";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half == nHalf - row)
                    {
                        arrChars2Half[col2Half] = "/";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half > nHalf - row)
                    {
                        // inner rombs
                        if (lastChar == " ")
                        {
                            arrChars2Half[col2Half] = "/";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == "/")
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        // inner rombs
                    }

                }   // for (int col2Half = 0; col2Half < nHalf; col2Half++)

                // second part of the string - backslash
                for (int col2Half = nHalf; col2Half < nWidth; col2Half++)
                {
                    if (col2Half < nHalf + row - 1)
                    {
                        // inner rombs
                        if (arrChars2Half[nHalf - 1] == "/" && col2Half == nHalf)
                        {
                            arrChars2Half[col2Half] = @"\";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (arrChars2Half[nHalf - 1] == " " && col2Half == nHalf)
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == @"\" && col2Half != nHalf)
                        {
                            arrChars2Half[col2Half] = " ";
                            lastChar = arrChars2Half[col2Half];
                        }
                        else if (lastChar == " " && col2Half != nHalf)
                        {
                            arrChars2Half[col2Half] = @"\";
                            lastChar = arrChars2Half[col2Half];
                        }
                        // inner rombs
                    }
                    else if (col2Half == nHalf + row - 1)
                    {
                        arrChars2Half[col2Half] = @"\";
                        lastChar = arrChars2Half[col2Half];
                    }
                    else if (col2Half > nHalf + row - 1)
                    {
                        arrChars2Half[col2Half] = ".";
                        lastChar = arrChars2Half[col2Half];
                    }

                }       // for (int col2Half = nHalf; col2Half < nWidth; col2Half++)

                //jaggedArray[row][arrChars2Half.Length] = new string[1, arrChars2Half.Length];

                //jaggedArray[row] = new string[nWidth-1];
                for (int i = 0; i < arrChars2Half.Length; i++)
                {
                    Console.Write(arrChars2Half[i]);
                    //jaggedArray[row][i] = arrChars2Half[i];
                }
                jaggedArray[row-1] = new string[nWidth - 1];
                jaggedArray[row-1] = arrChars2Half;
                Console.WriteLine();
            }   //for (int row = 1; row <= nHalf; row++)

            // to print jaggedArr
            for (int row = nHalf-1; row >= 0; row--)
            {
                for (int i = 0; i < nWidth; i++)
                {
                    if (jaggedArray[row][i] == @"\")
                    {
                        Console.Write("/");
                    }
                    else if (jaggedArray[row][i] == "/")
                    {
                        Console.Write(@"\");
                    }
                    else
                    {
                        Console.Write(jaggedArray[row][i]);
                    }
                }
                Console.WriteLine();
            }

        }   // end of carpets
Advertisements