2011-02-18 97 views
1

我的问题是如何在C#中使用*和'space'来制作金字塔?输出将是这样的。使用c制作金字塔#

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

我们只需要在这个程序中使用“for循环”。我只知道如何制作这个。

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

我犯了这样一个方案:

static void Main(string[]args) 
{ 
int i=o; 
int j=o; 

for(i=5;1>=1;i--) 
    for(j=1;j<=5;j++) 
    { 
    Console.Write("*"); 
    } 
    Console.WriteLine(" "); 
} 

我很困惑,当涉及到金字塔,因为它包含空格。谢谢你的帮助!

+0

很难帮助这个......莫st帮助就是答案... lemme认为... – hunter 2011-02-18 02:22:52

+10

其他人嫉妒他使用C#作业吗? – hunter 2011-02-18 02:23:33

+2

@hunter:怎么样?你期待他使用Assembly吗? – 2011-02-18 02:24:24

回答

12

想想你如何手动打印金字塔。

假设5层深。

1st line: 4 spaces, 1 star, 
2nd line: 3 spaces, star, space, star 
3rd line: 2 spaces, star space star space star 

不要紧,无论你最后一颗星星或不后打印的空间 - 不会对它的外观差异。

我们看到了什么?

如果我们有一个总的X水平

line 1: (x-1) spaces, (star space) 
line 2: (x-2) spaces, (star space) twice 
line 3: (x-3) spaces, (star space) three times 
line 4: (x-4) spaces, (star space) four times 

是这样的格局。我会把编码留给你。

2

你的问题是空间,所以我建议你考虑空间。告诉我这一点:第一颗星星左侧每行有多少个空间?如果你考虑这个问题,你很可能会解决你自己的问题。

2

尝试将其视为网格或矩阵,并查看每行中'*'的位置以及它与循环索引的关系。

1

对不起,我错过了,这是功课......将给予战略......而不是

它帮助,如果你在记事本中做到这一点,想想你在做什么?你会开始理解的关系在你所在的线和空间之间,什么不...

1

3小时后发布我的答案。我想现在你已经差不多完成了@ iluxa的建议呢?

int height = 20; 
for (int level = 1; level <= height; level++) 
{ 
    string text = string.Join(" ", Enumerable.Repeat("*", level)); 
    Console.WriteLine(text.PadLeft(height - level + text.Length)); 
} 

我使用了一些内置方法,例如, Enumerable.RepeatString.PadLeft,而不是纯粹的C语言方式。目的是为了告诉你,既然你选择了C#作为编程语言(而不是C/Java /等),你应该用C#方式解决问题。

3
using System; 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      int num, i, j, k; 
      Console.Write("enter the level:"); 
      num=Convert.ToInt32(Console.ReadLine()); 
      for (i = 1; i <= num; i++) 
      { 
       for (j = 1; j < num-i+1; j++) 
       { 
        Console.Write(" "); 
       } 
       for (k = 1; k <= i; k++) 
       { 
        Console.Write(i); 
        Console.Write(" "); 
       } 
       Console.WriteLine(); 

      } 
     } 
    } 
0
using System;    
using System.Collections.Generic;    
using System.Linq; 

using System.Text; 

namespace pyramid_star 

{ 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("enter a number:"); 
      int n = Convert.ToInt32(Console.ReadLine()); 
      for (int i = 1; i <= n; i++) 
      { 
       for (int x = i; x <= n; x++) 
       { 
        Console.Write(" "); 
       } 
       for (int j = 1; j <= i; j++) 
       { 
        Console.Write("*"+" "); 
       } 
       Console.WriteLine(); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 
1
using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Star_Pyramid 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program o = new Program(); 
      o.show(); 

      Console.ReadKey(); 
     } 
     void show() 
     { 
      for (int i = 1; i <= 12; i++) 
      { 
       for (int j = 1; j <= 9 - i/2; j++) 
       { 
        Console.Write(" "); 
       } 
       for (int k = 1; k <= i; k++) 
       { 
        Console.Write(" * "); 
        Console.Write(" "); 


       } 
       Console.WriteLine(); 

      } 
     } 
    } 
} 
1
class Program 

{ 

    static void Main(string[] args) 

    { 
     int num; 
     Console.WriteLine("enter level"); 
     num = Int32.Parse(Console.ReadLine()); 
     int count = 1; 

     for (int lines = num; lines >= 1; lines--) 
     { 

      for (int spaces = lines - 1; spaces >= 1; spaces--) 
      { 
       Console.Write(" "); 

      } 
      for (int star = 1; star <= count; star++) 
      { 
       Console.Write("*"); 
       Console.Write(" "); 

      } 
      count++; 

      Console.WriteLine(); 
     } 
     Console.ReadLine(); 
    } 
} 
1

试试这个,遵循同样的逻辑在C,C++,PHP,JAVA

using System; 

class pyramid { 

     static void Main() { 

      /** Pyramid stars Looking down 
       Comment this if u need only a upside pyramid **/ 

      int row, i, j; 

      // Total number of rows 
      // You can get this as users input 
      //row = Int32.Parse(Console.ReadLine()); 
      row = 5;    

      // To print odd number count stars use a temp variable 
      int temp; 
      temp = row; 

      // Number of rows to print 
      // The number of row here is 'row' 
      // You can change this as users input 
      for (j = 1 ; j <= row ; j++) { 

      // Printing odd numbers of stars to get 
      // Number of stars that you want to print with respect to the value of "i"?   
       for (i = 1 ; i <= 2*temp - 1 ; i++) 
        Console.Write("*"); 

      // New line after printing a row    
       Console.Write("\n"); 
       for (i = 1 ; i <= j ; i++)     
        Console.Write(" "); 

      // Reduce temp value to reduce stars count     
       temp--; 
      } 

      /** Pyramid stars Looking up 
       Comment this if u need only a downside pyramid **/ 
      int rowx, k, l; 

      // Total number of rows 
      // You can get this as users input 
      // rowx = Int32.Parse(Console.ReadLine()); 
      rowx = 5; 

      // To print odd number count stars use a temp variable 
      int tempx; 
      tempx = rowx; 

      //Remove this if u use 
      Console.Write("\n"); 

      // Number of rows to print 
      // The number of row here is 'rowx' 

      for (l = 1 ; l <= rowx ; l++) { 

      // Leaving spaces with respect to row 
       for (k = 1 ; k < tempx ; k++) 
        Console.Write(" "); 

      // Reduce tempx value to reduce space(" ") count 
       tempx--; 

      // Printing stars 
       for (k = 1 ; k <= 2*l - 1 ; k++) 
        Console.Write("*"); 

      // New line after printing a row 
       Console.Write("\n"); 
      }   
     } 
} 
1

下面的代码可能会有所帮助:

public static void print(int no) 
{ 
    for (int i = 1; i <= no; i++) 
    { 
     for (int j = i; j <= no; j++) 
     { 
      Console.Write(" "); 
     } 
     for (int k = 1; k < i * 2; k++) 
     { 
      if(k % 2 != 0) 
      { 
       Console.Write("*"); 
      } 
      else 
      { 
       Console.Write(" "); 
      } 
     } 
     Console.WriteLine(); 
    } 
    Console.ReadLine(); 
}