c++关于过程化语句

img


c++如何编程打印出如图九九乘法表上三角的形式。左上角有*号。

以下是 C++ 实现九九乘法表上三角形式的示例代码:

#include <iostream>
using namespace std;

int main() {
    cout << "* ";
    for (int i = 1; i <= 9; i++) {
        printf("%-2d ", i);
    }
    cout << endl;

    cout << "------------------------" << endl;

    for (int i = 1; i <= 9; i++) {
        cout << i << "| ";
        for (int j = 1; j <= i; j++) {
            printf("%-2d ", i * j);
        }
        cout << endl;
    }

    return 0;
}


其中,printf 函数用于控制输出宽度,并向左对齐。由于右对齐无法将左对齐的 * 和数字对齐,因此使用了向左对齐的方式。