Sierpinski Carpet by mashi

\xef\xbb\xbfusing System;\x0d
\x0d
namespace Sierpinsky_Carpet\x0d
{\x0d
    class Program\x0d
    {\x0d
        private bool[,] bits;\x0d
\x0d
        public static void Main()\x0d
        {\x0d
            int level = int.Parse(Console.ReadLine());\x0d
            Program p = new Program();\x0d
            p.PrintSierpinskiCarpet(level);\x0d
            p.Dump();\x0d
        }\x0d
\x0d
        void PrintSierpinskiCarpet(int level)\x0d
        {\x0d
            int size = (int)(Math.Pow(3, level));\x0d
            bits = new bool[size, size];\x0d
            PrintSierpinskiCarpet(level, 0, 0);\x0d
        }\x0d
\x0d
        void PrintSierpinskiCarpet(int level, int x, int y)\x0d
        {\x0d
            if (level == 0)\x0d
                bits[x, y] = true;\x0d
            else\x0d
                for (int i = 0; i < 9; i++)\x0d
                    if (i != 4)\x0d
                        PrintSierpinskiCarpet(level - 1, x * 3 + (i % 3), y * 3 + (i / 3));\x0d
        }\x0d
\x0d
        private void Dump()\x0d
        {\x0d
            int len = bits.GetUpperBound(0);\x0d
            for (int x = 0; x <= len; x++)\x0d
            {\x0d
                for (int y = 0; y <= len; y++)\x0d
                    Dot(bits[x, y]);\x0d
                Console.WriteLine();\x0d
            }\x0d
        }\x0d
\x0d
        static void Dot(bool fill)\x0d
        {\x0d
            if (fill)\x0d
                Console.Write("::");\x0d
            else\x0d
                Console.Write("  ");\x0d
        }\x0d
    }\x0d
}\x0d

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page