zoukankan      html  css  js  c++  java
  • 杨辉三角

    杨辉三角

    1. 递归方法

    package sort;

    /**
     * @author WangXiaoeZhe
     * @Date: Created in 2019/11/22 13:04
     * @description:
     */

    public class YangHuiF {
        public static int fun(int i, int j) {
            if (j == 1 || i == j) {
                return 1;
            } else {
                return fun(i - 1, j) + fun(i - 1, j - 1);
            }

        }

        public static void main(String[] args) {
            /**
             * 打印的行数
             */

            int length = 6;
            for (int i = 1; i <= length; i++) {

                /**
                 * 打印空格
                 */


                for (int j = 1; j <= length - i; j++) {
                    System.out.print("  ");
                }
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%4d"fun(i, j));
                }
                System.out.println();
            }
        }
    }

    2.非递归方法

    package sort;

    /**
     * @author WangXiaoeZhe
     * @Date: Created in 2019/11/22 13:22
     * @description:
     */

    public class YangHui {
        /**
         * 非递归的方法
         *
         * @param args
         */

        public static void main(String[] args{
            int length = 6;
            int[][] arr = new int[length][];

            for (int i = 0; i < arr.length; i++) {
                arr[i] = new int[i + 1];
                for (int j = 0; j < arr.length - i - 1; j++) {
                    System.out.print("  ");
                }

                for (int j = 0; j < arr[i].length; j++) {
                    if (j == 0 || arr[i].length - 1 == j) {
                        arr[i][j] = 1;
                    } else {
                        arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
                    }
                    System.out.printf("%4d", arr[i][j]);
                }
                System.out.println();
            }
        }

    }
  • 相关阅读:
    mysql 无法连接提示 Authentication plugin &#39;caching_sha2_password&#39; cannot be loaded
    探究分析:快速对大量的数据转换为数组
    SQL Server like 字段
    InfluxDB从原理到实战
    Python学习日记(四十) Mysql数据库篇 八
    MySQL数据库基本操作
    ES入门宝典(详细截图版)
    NameNode &amp;&amp; Secondary NameNode工作机制
    MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)
    mysql单个表拆分成多个表
  • 原文地址:https://www.cnblogs.com/wuhen8866/p/11911049.html
Copyright © 2011-2022 走看看