zoukankan      html  css  js  c++  java
  • 输出一下的杨慧三角(要求输出10行)

    输出一下的杨慧三角(要求输出10行)

    1   
    1   1
    1   2   1
    1   3   3   1
    1   4   6   4   1
    1   5  10  10   5   1
    ……
    

    【答案解析】

    仔细观察杨慧三角可以看到:

    第0列和对角线上的数据全部为1,其余位置上的数据为上一行正对数据与上一行正对前一个数据之和。

    比如:a[4][2] = a[3][2] + a[3][1]

    【代码实现】

    #include<stdio.h>
    int main()
    {
    	int array[10][10];
    	for (int i = 0; i < 10; ++i)
    	{
    		for (int j = 0; j <= i; ++j)
    		{
                // 对角线和第0列上全部为1
    			if (i == j || 0 == j)
    				array[i][j] = 1;
    			else
    				array[i][j] = array[i - 1][j] + array[i - 1][j - 1];
    		}
    	}
    
        // 打印杨慧三角的前10行
    	for (int i = 0; i < 10; ++i)
    	{
    		for (int j = 0; j <= i; ++j)
    		{
    			printf("%5d", array[i][j]);
    		}
    
    		printf("
    ");
    	}
    	return 0;
    }
    

    【结果截屏】

    输出一下的杨慧三角(要求输出10行)

  • 相关阅读:
    数组的地址和vector数组的地址
    字节跳动 测试开发工程师 面经
    最短路径树
    SPFA
    树的直径
    树的重心
    CF1401D Maximum Distributed Tree
    期望简述
    CF723E One-Way Reform
    CF1409E Two Platforms
  • 原文地址:https://www.cnblogs.com/inta/p/13330609.html
Copyright © 2011-2022 走看看