分析:
本题解决思路很多,这里提供一种比较直接的想法,将拿到的N拆分成可能的每一行所需打印的符号数目构成的list,余下来的自然就是不需要的符号
例如 19= [5,3,1,3,5] +2 ,之后按list中显示的各行对应的符号数依次打印出来即可
主要谈一谈,要通过PTA测试的注意事项,题主因为这两个坑爹原因提交了好久没通过,百思不得其解。。。
1. 打印出的沙漏形状,右侧千万不能出现多于的空格,在行尾请回车
2. 在调用python3 的input函数时,不能在屏幕上打印input的说明,否则也会被PTA判断成不通过。换句话说,input("") 可以通过测试,但是input("Please enter an integer")就会带来误判
代码如下:
#!/usr/bin/python
import sys
InputInfo = input ("")
InputList = InputInfo.split(' ', 1)
N = int(InputList[0])
Symbol = InputList[1]
container = N
if type(N) != type (1) or N <= 0 :
print("Something goes wrong, you didn't input a positive integer")
sys.exit()
count = 1
starnum_eachrow = [ 1 ]
container -= 1
rest_star = 0
while container > 0 :
count += 1
need_star_num= 2*count - 1
if container >= 2 * need_star_num :
container -= need_star_num*2
starnum_eachrow.append(need_star_num)
starnum_eachrow.insert(0, need_star_num)
else:
rest_star = container
container = 0
row_len = starnum_eachrow[0]
for starnum_thisrow in starnum_eachrow :
blank_space = int( ( row_len - starnum_thisrow )/2 )
this_row_print = " " *blank_space + Symbol * int( starnum_thisrow )
print (this_row_print)
print (rest_star, end = '' ) #To delete the automatic end \n