«

杨辉三角

灯点亮 发布于 阅读:51 C++


#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int a[10][10]{};
int y = sizeof(a) / sizeof(a[0]);
int x = sizeof(a[0]) / sizeof(a[0][0]);
int main() {
    for (int i = 0; i < x; i++) {
        a[i][0] = 1;
        a[i][i] = 1;
    }
    for (int i =2; i < y; i++) {
        for (int j = 1; j < i; j++) {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
        }

    }

    for (int i = 0; i < y; i++) {
        for (int j = 0; j < i+1; j++) {
            cout << a[i][j]<<'\t';
        }
        cout << endl;
    }

    system("pause");//卡屏函数
    return 0;
}