我用组合数的思路,60分
查看原帖
我用组合数的思路,60分
1585376
Zhouyiyijoey楼主2025/2/3 16:52
#include <iostream>
using namespace std;

// 计算组合数 C(x, y)
int fact(int x, int y) {
    int result = 1;
    // 计算分子部分
    for (int i = 0; i < y; ++i) {
        result *= (x - i);
    }
    // 计算分母部分
    for (int i = 1; i <= y; ++i) {
        result /= i;
    }
    return result;
}

int main() {
    int n;
    cin >> n;
    // 打印第一行
    cout << 1 << endl;
    // 从第二行开始打印
    for (int i = 2; i <= n; ++i) {
        // 每行的第一个数为 1
        cout << 1 << " ";
        // 计算并打印中间的数
        for (int j = 1; j < i - 1; ++j) {
            cout << fact(i - 1, j) << " ";
        }
        // 每行的最后一个数为 1
        cout << 1 << endl;
    }
    return 0;
}
2025/2/3 16:52
加载中...