我必须使用此规则打印数字金字塔:
奇数索引:1到索引,偶数索引:索引为1:
1
21
123
4321
12345
654321
1234567
87654321
123456789
Run Code Online (Sandbox Code Playgroud)
我写了这段代码:
def printFigure(rows):
if rows > 0:
if rows%2 == 0:
printFigure(rows-1)
while(rows>0):
print(str(rows)[::-1], end = '')
rows -= 1
print('')
if rows%2 == 1:
printFigure(rows-1)
while (rows>0):
print(str(rows),end = '')
rows -= 1
print('')
Run Code Online (Sandbox Code Playgroud)
但输出是:
1
21
321,
4321
54321
654321
7654321
87654321
987654321
Run Code Online (Sandbox Code Playgroud)
我是递归的初学者,我也很高兴你的解释.谢谢.
我有这个功能(在一个带有信号量的长程序中,如有必要,我会附上任何代码):
void signalHandler(int signumber){
/* for SIGINT */
if(signumber == SIGINT){
printf(" Pressed - Stop the timer & start cleaning process...\n");
flag = 0; //relevant for the rest of the program
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序并按下CTRL+C
停止它时,而不是获得以下输出:
^C Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - clean the queue*****
....
Run Code Online (Sandbox Code Playgroud)
我用双打印得到这个输出:
^C Pressed - Stop the timer & start cleaning process...
Pressed - Stop the timer & start cleaning process...
*****Timer finished / stopped - …
Run Code Online (Sandbox Code Playgroud)