c++关于过程化语句

img


用循环语句编程出一个梯形,最上面17个#号,往下依次减少一个,共八行

#include <iostream>

using namespace std;

int main() {
    int total_rows = 8;  // 总行数
    int max_width = 17;  // 最上面一行的字符数

    for (int row = 1; row <= total_rows; row++) {
        int num_chars = max_width - (row - 1);  // 当前行需要打印的字符数
        int num_spaces = max_width - num_chars;  // 当前行需要打印的空格数

        for (int i = 1; i <= num_chars; i++) {
            cout << "# ";
        }
        cout << endl;

        if (num_chars <= 1) {
            break;  // 如果已经只剩一个字符了,跳出循环
        }
    }

    return 0;
}

输出结果:

# # # # # # # # # # # # # # # # # 
 # # # # # # # # # # # # # # # #
  # # # # # # # # # # # # # # #
   # # # # # # # # # # # # # #
    # # # # # # # # # # # # #
     # # # # # # # # # # # #
      # # # # # # # # # # #
       # # # # # # # # # #

如果以上回答对您有所帮助,点击一下采纳该答案~谢谢