小编2OL*_*OL1的帖子

在 C 中向上增加 for 循环

我希望用 C 中的 range() 函数模拟 Python for 循环。我想完成一项任务,每次循环的次数越来越多,直到达到给定变量的值,在这种情况下为 5(对于变量 h)。这是在 Python 中的:

x = 5
y = 0
while x > y:
    for i in range(y+1):
        print("@",end='') 
    print('')
    y+=1

Output: 
@
@@
@@@
@@@@
@@@@@
Run Code Online (Sandbox Code Playgroud)

我能够在 C 中完成相反的事情(执行次数减少),如下所示:

{
    int h = 5;
    
    while (h > 0)
    {
        for (int i = 0; i < h; i++)
        {
            printf("@");
        }
        printf("\n");
        h--;
    }
}

Output:
@@@@@
@@@@
@@@
@@
@
Run Code Online (Sandbox Code Playgroud)

当我在 C 中尝试顶级版本时,随着执行次数的增加,我遇到了不知道如何控制各种递增和递减变量的问题。

c python loops

0
推荐指数
1
解决办法
60
查看次数

标签 统计

c ×1

loops ×1

python ×1