用C#创建星号树

时间:2011-04-06 18:31:57

标签: c# algorithm

大家好 我试着解决星号树问题 并发现我的代码无法正常工作,可以改进。

这是预期的输出

输入:5

    *
  * * *
* * * * *
  * * *
    *

输入:4

* * * *
  * *
* * * *

这是我的代码

static void Main(string[] args)
        {
            Console.Write("input:");

            char input = Console.ReadKey().KeyChar;

            if (char.IsDigit(input))
            {
                int couter = (int)char.GetNumericValue(input);

                Console.WriteLine();

                if (couter % 2 != 0)
                {

                    for (int i = 1; i <= couter; i++)
                    {

                        for (int j = 3; j > i; j--)
                        {

                            Console.Write("  ");

                        }

                        for (int k = 1; k <= i; k++)
                        {

                            Console.Write(" *");

                        }

                        Console.WriteLine();
                    }

                    for (int i = couter - 1; i >= 3; i--)
                    {
                        for (int j = 1; j <= i; j++)
                        {

                            if (j <= couter - i)
                            {
                                Console.Write("  ");
                            }
                            else
                            {
                                Console.Write("* ");
                            }
                        }

                        Console.WriteLine();
                    }

                }
                else
                {

                    for (int i = couter; i > 3; i--)
                    {
                        for (int j = 1; j <= i; j++)
                        {
                            if (couter - i >= j)
                            {
                                Console.Write("  ");
                            }
                            else
                            {
                                Console.Write("* ");
                            }
                        }

                        Console.WriteLine();


                    }

                    for (int i = couter - 1; i <= couter; i++)
                    {
                        for (int j = 0; j < i; j++)
                        {
                            Console.Write("* ");
                        }

                        Console.WriteLine();
                    }
                }

            }
        }

请你帮我解决这个问题。

最近,我认为我在算法方面很差,而且有点复杂的问题。有人知道有用的链接或我如何提高这项技能,请告诉我。

谢谢,

3 个答案:

答案 0 :(得分:1)

检查此页面输入5(菱形):http://www.dreamincode.net/forums/topic/126715-diamond-asterisk/

我已将其翻译为C# - 现在它会显示您在变量“行”中设置的大小的钻石:

int rows = 5;
StringBuilder sb = new StringBuilder();
// top part
for (int i = 1; i <= rows; i++)
{
    for (int j = 1; j <= rows - i; j++)
        sb.Append(' ');
    for (int k = 1; k <= 2 * i - 1; k++)
        sb.Append('*');
    sb.AppendLine();
}
//bottom part
for (int n = rows - 1; n > 0; n--)
{
    for (int l = 1; l <= rows - n; l++)
        sb.Append(' ');
    for (int m = 1; m <= 2 * n - 1; m++)
        sb.Append('*');
    sb.AppendLine();
}
Console.Write(sb.ToString());

答案 1 :(得分:1)

我最初不愿发布它,因为它肯定闻起来像家庭作业......

无论如何,这是一段工作代码:

static void Main(string[] args)
{
    Console.Write("input:");

    char input = Console.ReadKey().KeyChar;

    if (char.IsDigit(input))
    {
        int couter = (int)char.GetNumericValue(input);
        Console.WriteLine();
        if (couter % 2 != 0)
            PrintDiamond(couter);
        else
            PrintHourGlass(couter);
    }
    Console.ReadLine();
}

private static void PrintDiamond(int couter)
{
    bool moreAsterisks = true;
    for (int row = 0; row < couter; row++)
    {
        int nAsterisks = moreAsterisks ? (2 * row) + 1 : 2 * (couter - row - 1) + 1;
        int nSpaces = (couter - nAsterisks) / 2;

        if (row == (couter - 1) / 2)
            moreAsterisks = false;

        for (int i = 0; i < nSpaces; i++)
            Console.Write(" ");
        for (int i = 0; i < nAsterisks; i++)
            Console.Write("*");
        for (int i = 0; i < nSpaces; i++)
            Console.Write(" ");
        Console.WriteLine();
    }
}

private static void PrintHourGlass(int couter)
{
    bool moreAsterisks = false;
    for (int row = 0; row < couter - 1; row++)
    {
        int nAsterisks = moreAsterisks ? couter - 2 * (couter - row - 2) : couter - (2 * row);
        int nSpaces = (couter - nAsterisks) / 2;

        if (row == (couter - 2) / 2)
            moreAsterisks = true;

        for (int i = 0; i < nSpaces; i++)
            Console.Write(" ");
        for (int i = 0; i < nAsterisks; i++)
            Console.Write("*");
        for (int i = 0; i < nSpaces; i++)
            Console.Write(" ");
        Console.WriteLine();
    }
}

P.S.: 它适用于任何数字,而不仅仅是4-5 ......

答案 2 :(得分:0)

这是针对您的问题的缩小LINQ解决方案:

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("input: ");
            string line = Console.ReadLine();

            int n;
            if (!int.TryParse(line, out n))
            {
                Console.WriteLine("Enter a valid integer number.");
                return;
            }

            for (int i = 0; i < n; i++)
            {
                int l = Math.Abs(n - i * 2 - 1) + 1;
                if (n % 2 != 0) l = n - l + 1;

                Console.Write(Enumerable.Repeat(" ", n - l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
                Console.WriteLine(Enumerable.Repeat("* ", l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
            }
        }
    }

这很简单;在循环内部,如果输入是偶数,则第一行计算第i个菱形行的长度,第二行校正奇数输入的计算。其余两行使用一些LINQ技巧打印第i行。如果您不喜欢使用LINQ进行炫耀,可以使用常规for循环替换thoose行(这可能会更快)。然后代码看起来像:

class Program
{
    static void Main(string[] args)
    {
        Console.Write("input: ");
        string line = Console.ReadLine();

        int n;
        if (!int.TryParse(line, out n))
        {
            Console.WriteLine("Enter a valid integer number.");
            return;
        }

        for (int i = 0; i < n; i++)
        {
            int l = Math.Abs(n - i * 2 - 1) + 1;
            if (n % 2 != 0) l = n - l + 1;

            //Console.Write(Enumerable.Repeat(" ", n - l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
            //Console.WriteLine(Enumerable.Repeat("* ", l).DefaultIfEmpty("").Aggregate((a, b) => a + b));
            for (int c = 0; c < n - l; c++) Console.Write(" ");
            for (int c = 0; c < l; c++) Console.Write("* ");
            Console.WriteLine();
        }
    }
}