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

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

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

问题

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

  1. 我正在尝试在.Net 6C#)上创建一个控制台应用程序,我需要根据用户输入的字符数量构建三角形,而不是用户输入的行数。
  2. 我尝试通过嵌套的for()循环来实现一个简单的三角形绘制,但我的金字塔不是由6个字符绘制的,而是更多,多达10个。我尝试以不同的方式实现循环,创建一个新变量或添加另一个while循环作为常数,但都没有奏效。
  3. 我附上了下面的示例代码:
  4. static void Main()
  5. {
  6. Console.WriteLine("输入要构建金字塔的符号数量:");
  7. int p = Convert.ToInt32(Console.ReadLine());
  8. for (int i = 0; i < p; i++)
  9. {
  10. Console.WriteLine();
  11. for (int j = 0; j <= i; j++)
  12. {
  13. Console.Write("*");
  14. }
  15. }
  16. }
英文:

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:

  1. static void Main()
  2. {
  3. Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);
  4. int p = Convert.ToInt32(Console.ReadLine());
  5. for (int i = 0; i &lt; p; i++)
  6. {
  7. Console.WriteLine();
  8. for (int j = 0; j &lt;= i; j++)
  9. {
  10. Console.Write(&quot;*&quot;);
  11. }
  12. }
  13. }

答案1

得分: 2

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

  1. (1) *
  2. (2) **
  3. (3) ***
  4. ... ...
  5. (m - 1) *** ... *
  6. (m) *** ... **

所以点的总数 (n)

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

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

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

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

  1. private static bool DrawTriangle(int n) {
  2. if (n <= 0)
  3. return false;
  4. // 我们需要整数解(四舍五入),而不是浮点数解
  5. long m = (long)((-1.0 + Math.Sqrt(1.0 + 8.0 * n)) / 2.0);
  6. long residue = n - m * (m + 1) / 2;
  7. for (int i = 1; i <= m; ++i)
  8. Console.WriteLine(new string('*', i));
  9. if (residue > 0)
  10. Console.WriteLine(new string('*', (int)residue));
  11. return true;
  12. }

用法:

  1. static void Main() {
  2. Console.WriteLine("输入要构建金字塔的符号数量:");
  3. // TODO: 使用int.TryParse是更好的技巧
  4. int p = Convert.ToInt32(Console.ReadLine());
  5. if (!DrawTriangle(p))
  6. Console.WriteLine("不是正数");
  7. }

Fiddle

英文:

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

  1. (1) *
  2. (2) **
  3. (3) ***
  4. ... ...
  5. (m - 1) *** ... *
  6. (m) *** ... **

So the total number of points (n)

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

If we solve it for m we will get

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

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

  1. private static bool DrawTriangle(int n) {
  2. if (n &lt;= 0)
  3. return false;
  4. // we want integer solution (rounded), not a floating point one
  5. long m = (long)((-1.0 + Math.Sqrt(1.0 + 8.0 * n)) / 2.0);
  6. long residue = n - m * (m + 1) / 2;
  7. for (int i = 1; i &lt;= m; ++i)
  8. Console.WriteLine(new string(&#39;*&#39;, i));
  9. if (residue &gt; 0)
  10. Console.WriteLine(new string(&#39;*&#39;, (int)residue));
  11. return true;
  12. }

usage:

  1. static void Main() {
  2. Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);
  3. // TODO: int.TryParse is a better technique
  4. int p = Convert.ToInt32(Console.ReadLine());
  5. if (!DrawTriangle(p))
  6. Console.WriteLine(&quot;Not a positive number&quot;);
  7. }

Fiddle

答案2

得分: 1

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

  1. *
  2. **
  3. **

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

  1. Console.WriteLine("输入要构建金字塔的符号数量:");
  2. int p = Convert.ToInt32(Console.ReadLine());
  3. int lineSize = 1;
  4. for (int i = 0; i < p; i++)
  5. {
  6. for (int j = 0; j < lineSize; j++)
  7. {
  8. if (j + i < p)
  9. Console.Write("*");
  10. }
  11. i = i + lineSize - 1;
  12. Console.WriteLine();
  13. lineSize++;
  14. }

结果:

  1. 输入要构建金字塔的符号数量:
  2. 5
  3. *
  4. **
  5. **
  6. 输入要构建金字塔的符号数量:
  7. 6
  8. *
  9. **
  10. ***
  11. 输入要构建金字塔的符号数量:
  12. 10
  13. *
  14. **
  15. ***
  16. ****
  17. 输入要构建金字塔的符号数量:
  18. 11
  19. *
  20. **
  21. ***
  22. ****
  23. *
英文:

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

  1. *
  2. **
  3. **

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

  1. Console.WriteLine(&quot;Enter the number of symbols to build pyramid: &quot;);
  2. int p = Convert.ToInt32(Console.ReadLine());
  3. int lineSize = 1;
  4. for (int i = 0; i &lt; p; i++)
  5. {
  6. for (int j = 0; j &lt; lineSize; j++)
  7. {
  8. if(j+i&lt;p)
  9. Console.Write(&quot;*&quot;);
  10. }
  11. i = i + lineSize - 1;
  12. Console.WriteLine();
  13. lineSize++;
  14. }

result:

  1. Enter the number of symbols to build pyramid:
  2. 5
  3. *
  4. **
  5. **
  6. Enter the number of symbols to build pyramid:
  7. 6
  8. *
  9. **
  10. ***
  11. Enter the number of symbols to build pyramid:
  12. 10
  13. *
  14. **
  15. ***
  16. ****
  17. Enter the number of symbols to build pyramid:
  18. 11
  19. *
  20. **
  21. ***
  22. ****
  23. *

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:

确定