首页 > 其他分享 >杨辉三角

杨辉三角

时间:2023-01-14 17:22:54浏览次数:63  
标签:%- int vector printf 杨辉三角 row

百度百科:杨辉三角 | 刘徽

#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] for x +
  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);
 应为r.insert(r.begin(), 0);
 r.insert(0, 0); VC6,Warning Level 3: 没警告没错误,一运行就崩
*/

 

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

标签:%-,int,vector,printf,杨辉三角,row
From: https://www.cnblogs.com/funwithwords/p/17052109.html

相关文章

  • 杨辉三角的5个特性
    作者:小傅哥博客:https://bugstack.cn沉淀、分享、成长,让自己和他人都能有所收获!......
  • 杨辉三角的5个特性,一个比一个牛皮!
    作者:小傅哥博客:https://bugstack.cn沉淀、分享、成长,让自己和他人都能有所收获!......
  • LeetCode刷题(52)~杨辉三角【看似简单???】
    题目描述给定一个非负整数numRows,生成杨辉三角的前numRows行。在杨辉三角中,每个数是它左上方和右上方的数的和。示例:输入:5输出:[[1],[1,1],[1......
  • 2023.1.06 java打印杨辉三角(二维数组)
    publicclassyanghui{publicstaticvoidmain(String[]args){int[][]yanghui=newint[10][];for(inti=0;i<yanghui.length;i++){......
  • 【LeeCode】118. 杨辉三角
    【题目描述】给定一个非负整数numRows,生成「杨辉三角」的前numRows行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。​​https://leetcode.cn/problems/pascals-......
  • 杨辉三角
    ProblemDescription还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:11112113311464115101051  Input输入数据包含多个......
  • leetcode_D8_118杨辉三角
    1.题目  2.解一  主要思路:这个一看就看懂,没啥好说的。3.解二  主要思路:评论区看到的聪明解法,即10        011       01......
  • 打印杨辉三角形
    我使用的是递归法#include<stdio.h>inty(intk,intl){if(l==0||k==l)return1;elsereturny(k-1,l)+y(k-1,l-1);}intmain(){inti,m,......
  • 打印如下图形的杨辉三角
    图形如下   代码如下1#define_CRT_SECURE_NO_WARNINGS12#include<stdio.h>3intmain()4{56intarr[11][10];//打印这种图形第0行要舍弃......
  • java中的杨辉三角
    本文主要介绍如何打印杨辉三角(直角三角形),如下图所示: 规律如下:第一行全为1,对角线元素全为1,设i表示行标,j表示列标。arr[i][0]=1;arr[i][i]=1;当i>0,j>0时......