0
L = [[" " for i in range(10)] for j in range(10)]

for i in range (10):
    L[9][i]="*"
for i in range (10):
        L[8][i]="1"
for i in range (10):
        L[7][i]="*"
for i in range (10):
        L[6][i]="3"
for i in range (10):
        L[5][i]="*"

print(L)
print()

def Check_Lines(l):
    for i in range (10):
        x=l[i].count("*")
        if x == 10:
            print ("LINE IS FULL")
            del l[i]
            l.reverse()
            l.append([" "," "," "," "," "," "," "," "," "," "])
            l.reverse()


Check_Lines(L)
print (L)

由于对python语言比较熟悉,所以我用python写了上面的函数。它所做的是搜索一个 10 x 10 的列表,如果单行用 * 填充,那么它将删除它并在顶部放置一个新的空行。

我知道 c 没有我使用的列表函数。有什么简单的方法可以解决我正在做的事情吗?

4

1 回答 1

1

不幸的是,在 C 中,这个问题将非常困难。需要您为矩阵分配一个内存块。当你想“删除”一行时,你必须向上移动所有内容并释放矩阵底部的额外内存。

如果您可以使用 C++,则使用 STL 容器更容易做到这一点。你可以有一个向量的向量。删除一行将是对擦除的调用:

l.erase(pos)  // pos is an iterator at the row you'd like to remove
于 2013-06-28T18:03:23.720 回答