我一直在努力将这个 while 嵌套循环转换为 while only 嵌套循环

问题描述 投票:0回答:1
int rows = 5;
int row = rows;

    while (row >= 1) {
        for (int space = 1; space <= rows - row; ++space) {
            cout << " ";
        }
        for (int star = 1; star <= row; ++star) {
            cout << "*";
        }
        cout << endl; 
        row--; 
    }

结果应该是这样的inverted triangle

c++ visual-c++
1个回答
0
投票

给你,只要

while
,哪怕只是一会儿,为了让你得到老师的特别关注。

#include <iostream>

int main() {
    
    int place=1;
    char output='*';
    while (place<=25)
    {
        printf("%c", output);
        if (!(place%5))
        {
            printf("\n");
            output=' ';
        }
        if (!(place%6)) output='*';
        place++;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.