这里我是如何这样做的。 检查一些样本钻石(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
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*