English 中文(简体)
印刷ASCII手工钻石
原标题:Print an ASCII art diamond
  • 时间:2009-10-09 02:07:05
  •  标签:

这是我为我的学校方案拟订课程提出的家庭工作问题,而我失去了这种机会。 这里的问题是:

Write an application program that prints a diamond of asterisks (*) on the screen. The number of lines in the diamond is given by the user. As an example, if the user requested a diamond with 7 lines, the following would be displayed.

这就是我迄今为止所做的事情:

{
  int nlines;
  int nsp;
  cout << "enter the number of lines (must be more then one)" << endl;
  cin >> nlines;
  while((nlines)<=0)
  {
    cout << "enter the number of lines (must be more then one)" << endl;
    cin >> nlines;
  }
  nsp=(nlines - 1)/2;
  for(int currspace = 1; currspace <= nsp; currspace++)
  {
    cout << " ";
  }
  cout << "*" << endl;
  for( int currentline = 0; currentline < nlines; currentline++)
  {
    for( int currentaster = 0; currentaster <= currentline; currentaster++)
      cout << "*";
    cout << endl;
  }
  return 0;
问题回答

我在努力作出彻底的回答,因为它是家务劳动,但我的直接建议是努力把任务分成一些小任务,每个次级任务都比较简单。 例如,我开始履行一个小的职能,除了在one <>>one上印出一定长度的星号,并附有具体理由。 从事情的角度看,你可能很容易地处理这种问题,而你有的话,其余部分则比较简单。

在做家务时,我只建议你可能要采取的一些步骤,并且让你能够实际撰写:

因此,为了印制钻石,我假定,它就好像这样(例如,有5条线——线数必须奇怪)。

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

我以身作则。 因此,你需要做的是确定需要多少空间,然后是多少星号。 在每一行之后,根据目前线数,确定你需要增加/替代的空间,然后确定你们需要增加/替代的数量。 我建议采取一些步骤:

  1. Write a function that calculates the number of spaces to print, given the total number of lines and the current line number
  2. Write a similar function for number of asterisks
  3. Use these two functions to print out the diamond, line by line.

或许一个良好的开端是,你可以写上屏幕上的n空间和星。

这里我是如何这样做的。 检查一些样本钻石(5、7和9条线):

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

第1行包括一些空间和一些星体。 有多少? 恒星数始终为一,空间数取决于所希望的线数:

Number of lines  |  Initial space count
-----------------+---------------------
       1         |            0
       2         |            0
       3         |            1
       4         |            1
       5         |            2
       6         |            2
       7         |            3
       8         |            3

因此,第2版的编号为:<><>>>>>>>>>>。

你们注意到的另一件事是,在随后的每一条线路上,空间数量减少了1个,星号增加了2个。

直到空间数量为零为止,你开始将空间增加一个,将星体减少两个,直到星号再次出现。

唯一另一个特别情况是,你应该重复中线。

看看你的代码,请上 各项 > 结束,你只得调整在什么地方的发言与正在印发的发言稿有关。 换言之,外部环形是指线,然后是空间的内部通道,然后是星体内的另一环。

我在沙尔写道了一个试验方案(见下文),但你真的必须理解沙尔·辛塔克斯(该方案比我原先认为的要大得多),才能从这一答案中得到某种使用,这样就能够形成一些简化的假象。 您在开始起草法典之前,应先坐下来,思考问题。 这将有助于你发展今后将非常好的工作技能。

Main:
    Get numlines from user, check that greater than 0.
    Set numstars to 1.
    Set numspaces to int((numlines-1)/2)
    call Output (numspaces,numstars)
    while numspaces > 0:
        numspaces = numspaces - 1
        numstars = numstars + 2
        call Output (numspaces,numstars)
    if numlines is even:
        call Output (numspaces,numstars)
    while numstars > 0:
        numspaces = numspaces + 1
        numstars = numstars - 2
        call Output (numspaces,numstars)
    end.

Output(spaces,stars):
    for i = 1 to spaces:
        print " "
    for i = 1 to stars:
        print "*"
    print end-of-line
    return

最后,在座右边,我用来测试假装编码(实际上没有给你,因为你需要C++):

import sys

# Construct line based on number of spaces and stars.
def outLine (spc,str):
    # Start with empty line.
    line = ""

    # Add spaces.
    for i in range(0,spc):
        line = "%s "%(line)

    # Add stars.
    for i in range(0,str):
        line = "%s*"%(line)

    #Output line.
    print line

# Get number of lines from user and check.

numlines = input ("Enter number of lines: ")
if numlines < 1:
    print "Must be greater than zero"
    sys.exit(1);

# Calculate initial space and star count.
numspaces = int ((numlines-1)/2)
numstars = 1

# Output initial line.
outLine (numspaces,numstars)

# Output subsequent lines until middle reached.
while numspaces > 0:
    numspaces = numspaces - 1
    numstars = numstars + 2
    outLine (numspaces,numstars)

# Repeat middle if even number of lines desired.
if numlines % 2 == 0:
    outLine (numspaces,numstars)

# Output the bottom half of the diamond.
while numstars > 0:
    numspaces = numspaces + 1
    numstars = numstars - 2
    outLine (numspaces,numstars)

例如:

Enter number of lines: 15
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

其他人则表示,你通过制定有用的路线来设计“自下而上”。

另一种做法是设计一个“自上而下”的方案:首先,通过撰写守则,假定任何/所有有助于实施解决办法的有用小小部分已经存在:这样做将使你能够决定你想要的哪些次长。

例如:

int main()
{
  int nLinesTotal = getNumberOfLinesFromUser();
  int nLinesTopHalf = getHalfOf(nLinesTotal);
  //caution: using 1-based not 0-based index here
  for (int i = 1; i <= nLinesTopHalf; ++i)
  {
    int nAsterisksOnThisLine = calculateAsterisks(i);
    //following function takes nLinesTotal as a parameter because
    //that s necessary to calculate how wide the diamon is and
    //therefore how much leading whitespace there is on each line
    printAsterisks(nLinesTotal, nAsterisksOnThisLine);
  }
  ... etc for the middle line
  ... and for the bottom half of the diamond
}

这样做表明,像以下方式那样的分行是有益的:

  • getNumberOfLinesFromUser
  • getHalfOf
  • calculateAsterisks
  • printAsterisks

举一个例子。 钻石箱的体积高度为9。 下图显示如何安排Xy坐标表***。

alt text http://img527.imageshack.us/img527/7261/57257125.png

首先,我们需要把原产地移至钻石中心,具体如下。

alt text http://img515.imageshack.us/img515/7122/89173144.png

确定所有坐标的绝对值。 然后,数字如下。

alt text http://img73.imageshack.us/img73/9550/75806473.png

以上数字是解决问题的办法。

Loop x and y from 0 to 8. 
Print  *  if (x+y) <= 4;  
else Print a space (   );

现在就有一个一般性案例。 然后,我们必须

loop from 0 to (Height - 1)
Print  *  if (x+y) <= (Height/2);  
else Print a space (   );

<>Program>:

void main()
{
    int Height = 15;

    for( int y = 0; y < Height; y++ )
    {
        for( int x = 0; x < Height; x++ )
        {
            int X = abs( x - ( Height / 2 ) );
            int Y = abs( y - ( Height / 2 ) );

            if( ( X + Y ) <= ( Height / 2 ) )
                cout <<  * ;
            else
                cout <<    ;
        }
        cout << endl;
    }
}

<<>Output>:

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




相关问题
热门标签