如何根据用户输入的字符数量绘制一个三角形?

huangapple go评论77阅读模式
英文:

How to draw a triangle from the number of characters entered by the user?

问题

以下是您要的代码部分的翻译:

我正在尝试在.Net 6C#)上创建一个控制台应用程序,我需要根据用户输入的字符数量构建三角形,而不是用户输入的行数。

我尝试通过嵌套的for()循环来实现一个简单的三角形绘制,但我的金字塔不是由6个字符绘制的,而是更多,多达10个。我尝试以不同的方式实现循环,创建一个新变量或添加另一个while循环作为常数,但都没有奏效。

我附上了下面的示例代码:

static void Main()
{
    Console.WriteLine("输入要构建金字塔的符号数量:");

    int p = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i < p; i++)
    {
        Console.WriteLine();

        for (int j = 0; j <= i; j++)
        {
            Console.Write("*");
        }
    }
}
英文:

I'm trying to make a console application on .Net 6 (C#), I need the triangle to be built from the number of characters entered by the user, and not the number of LINES entered by the user.

I tried to implement a simple rendering of a triangle by timing through a nested for() loop, but my pyramid is drawn not from 6 characters, but more, from 10. I tried to implement the loop in different ways, create a new variable or add another while loop as a constant, but nothing worked.

I attached an example code below:

static void Main()
{
    Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);

    int p = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i &lt; p; i++)
    {
        Console.WriteLine();

        for (int j = 0; j &lt;= i; j++)
        {
            Console.Write(&quot;*&quot;);
        }
    }
}

答案1

得分: 2

让我们从数学角度开始:找出我们何时可以绘制一个具有 m 行的三角形:

(1)     *
(2)     **
(3)     ***
...     ...
(m - 1) *** ... *
(m)     *** ... ** 

所以点的总数 (n)

1 + 2 + 3 + ... + m = m * (m + 1) / 2 = n

如果我们解出 m 的值,我们可以得到

m^2 + m - 2 * n = 0

m = (-1 + sqrt(1 + 8 * n)) / 2

因此,我们可以解出 m,如果存在一个整数解,就可以绘制这个三角形。

private static bool DrawTriangle(int n) {
  if (n <= 0)
    return false;

  // 我们需要整数解(四舍五入),而不是浮点数解
  long m = (long)((-1.0 + Math.Sqrt(1.0 + 8.0 * n)) / 2.0);
		
  long residue = n - m * (m + 1) / 2;

  for (int i = 1; i <= m; ++i)
    Console.WriteLine(new string('*', i));
		
  if (residue > 0)
    Console.WriteLine(new string('*', (int)residue));

  return true;
}

用法:

static void Main() {
  Console.WriteLine("输入要构建金字塔的符号数量:");

  // TODO: 使用int.TryParse是更好的技巧
  int p = Convert.ToInt32(Console.ReadLine());

  if (!DrawTriangle(p))
    Console.WriteLine("不是正数");
}

Fiddle

英文:

Let's start from the math: let's find when we can draw a triangle of m lines:

(1)     *
(2)     **
(3)     ***
...     ...
(m - 1) *** ... *
(m)     *** ... ** 

So the total number of points (n)

1 + 2 + 3 + ... + m = m * (m + 1) / 2 = n

If we solve it for m we will get

m^2 + m - 2 * n = 0

m = (-1 + sqrt(1 + 8 * n)) / 2

So we can solve for m and if an integer solution exists, draw the triangle

private static bool DrawTriangle(int n) {
  if (n &lt;= 0)
    return false;

  // we want integer solution (rounded), not a floating point one
  long m = (long)((-1.0 + Math.Sqrt(1.0 + 8.0 * n)) / 2.0);
		
  long residue = n - m * (m + 1) / 2;

  for (int i = 1; i &lt;= m; ++i)
    Console.WriteLine(new string(&#39;*&#39;, i));
		
  if (residue &gt; 0)
    Console.WriteLine(new string(&#39;*&#39;, (int)residue));

  return true;
}

usage:

static void Main() {
  Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);

  // TODO: int.TryParse is a better technique
  int p = Convert.ToInt32(Console.ReadLine());

  if (!DrawTriangle(p))
    Console.WriteLine(&quot;Not a positive number&quot;);
}

Fiddle

答案2

得分: 1

用户输入的数字必须能够构建一个三角形。例如,对于数字5,是不可能的:

*
**
**

但如果输入的数字有效,您可以使用以下代码:

Console.WriteLine("输入要构建金字塔的符号数量:");
int p = Convert.ToInt32(Console.ReadLine());

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

结果:

输入要构建金字塔的符号数量:
5
*
**
**
输入要构建金字塔的符号数量:
6
*
**
***
输入要构建金字塔的符号数量:
10
*
**
***
****
输入要构建金字塔的符号数量:
11
*
**
***
****
*
英文:

The number that the user enters must be able to make a triangle. For
example, it is not possible for the number 5:

*
**
**

But if the entered number is valid, you can use the following code:

    Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);
    int p = Convert.ToInt32(Console.ReadLine());

    int lineSize = 1;
    for (int i = 0; i &lt; p; i++)
    {
         for (int j = 0; j &lt; lineSize; j++)
         {
             if(j+i&lt;p)
                 Console.Write(&quot;*&quot;);
         }
         i = i + lineSize - 1;
         Console.WriteLine();
         lineSize++;
    }

result:

Enter the number of symbols to build pyramid:
5
*
**
**
Enter the number of symbols to build pyramid:
6
*
**
***
Enter the number of symbols to build pyramid:
10
*
**
***
****
Enter the number of symbols to build pyramid:
11
*
**
***
****
*

huangapple
  • 本文由 发表于 2023年2月23日 21:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定