as12350992 发表于 2020-11-29 22:13:06

C++练习题7

本帖最后由 as12350992 于 2020-11-29 23:42 编辑

Activity 12
Printing Pyramids
Print a pyramid of Pascal’s triangle
Input number of rows: 5                                             
          1                                                            
      1   1                                                         
      1   2   1                                                      
    1   3   3   1                                                      
1   4   6   4   1
活动12
印刷金字塔
打印帕斯卡三角形的金字塔
输入行数:5
          1个
      1 1
      1 2 1
    1 3 3 1
1 4 6 4 1

贾政景 发表于 2021-7-25 17:24:09

没有解析吗?

浮世醉心 发表于 2021-7-26 11:40:55

#include<iostream>
using namespace std;

int main()
{
    int a, b = 1;

    cout << "Enter number : ";
    cin >> a;

    for (int i = 0; i < a; i++)
    {
      for (int c = 1; c <= a - i; c++)
            cout << "";

      for (int j = 0; j <= i; j++)
      {
            if (j == 0 || i == 0)
                b = 1;
            else
                b = b * (i - j + 1) / j;

            cout <<b << "   ";
      }
      cout << endl;
    }

    return 0;
}
页: [1]
查看完整版本: C++练习题7