杨辉三角

百度百科【杨辉三角刘徽数学归纳法排列组合】 在线运行C++

                1   1
              1   2   1
            1   3   3   1
          1   4   6   4   1
        1   5   10  10  5   1
      1   6   15  20  15  6   1
    1   7   21  35  35  21  7   1
  1   8   28  56  70  56  28  8   1
1   9   36  84  126 126 84  36  9   1
#include <cstdio>
#include <vector> // https://cplusplus.com/reference/vector/vector/
using namespace std;

enum { N = 10, W = N * 4 };

typedef vector<int>  row;

void print(int n, const row& r) {
  int  w = n * 4;
  if (int left = (W - w) / 2) printf("%-*c", left, ' ');
  for (int i = 0; i < r.size(); i++) printf("%-4d", r[i]);
  puts("");
}

int main() {
  row r(2, 1); // [1, 1] (x + y)
  for (int n = 2; n <= N; n++) {
    print(n, r);
    // (x + y) * (xx + 2xy + yy) =
    // xxx + 2xxy +  xyy +         1 2 1 0
    //        xxy + 2xyy + yyy     0 1 2 1
    const int m = r.size(); r.push_back(0);
    row t = r; t.insert(t.begin(), 0);
    for (int i = 0; i <= m; i++) r[i] += t[i];
  }
  getchar(); return 0;
}
/*
 iterator insert (iterator position, const value_type& val);
 应为t.insert(t.begin(), 0);
 t.insert(0, 0); VC6,Warning Level 3: 没警告没错误,一运行就崩
*/

令x=y=1,可得2n等于?n个灯,从全亮到全灭,亮=1, 灭=0,有多少种组合?

posted @ 2023-01-14 17:15  Fun_with_Words  阅读(60)  评论(0编辑  收藏  举报









 张牌。