簡體   English   中英

用C#創建星號樹

[英]create asterisk tree with C#

大家好我試着解決星號樹問題,發現我的代碼工作不正常,可以改進。

這是預期的輸出

輸入: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();
                    }
                }

            }
        }

請你幫我解決這個問題。

最近,我認為我在算法方面很差,而且有點復雜的問題。 有人知道有用的鏈接或我如何提高這項技能,請告訴我。

謝謝,

檢查此頁面輸入5(菱形): http//www.dreamincode.net/forums/topic/126715-diamond-asterisk/

我已將其翻譯為C# - 現在它顯示的鑽石大小可以在變量'rows'中設置:

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());

我最初不願發布它,因為它絕對聞起來像家庭作業......

無論如何,這是一段工作代碼:

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();
    }
}

PS :它適用於任何數字,而不僅僅是4-5 ......

這是針對您的問題的縮小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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM