杨辉三角

百度百科【杨辉三角刘徽数学归纳法排列组合】 在线运行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,有多少种组合?

原文链接: https://www.cnblogs.com/funwithwords/p/17052109.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    杨辉三角

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/311993

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:13
下一篇 2023年2月16日 下午12:14

相关推荐