打印出杨辉三角形(要求打印出10行如下图)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

public class Example33 {

public static void main(String[] args) {
        yanghui(10);
    }

public static void yanghui(int n) {
        int[][] a = new int[n][n];
        for (int i = 0; i < n; i++) {
            a[i][i] = 1;
            a[i][0] = 1;
        }
        for (int i = 2; i < n; i++) {
            for (int j = 1; j < i; j++) {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int k = 0; k < 2 * (n - i) - 1; k++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(a[i][j] + "   ");
            }
            System.out.println();
        }
    }
}

Java经典编程题50道之三十三的更多相关文章

  1. Java经典编程题50道之三十六

    有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数. public class Example36 {    public static void main(String[] a ...

  2. Java经典编程题50道之四十三

    一个偶数总能表示为两个素数之和. public class Example43 {    public static void main(String[] args) {        f();   ...

  3. Java经典编程题50道之三十九

    写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class Example39 {    public static void main(String[] a ...

  4. Java经典编程题50道之三十八

    编写一个函数:输入n为偶数时,调用函数求1/2+1/4+...+1/n:当输入n为奇数时,调用函数1/1+1/3+...+1/n. public class Example38 {    public ...

  5. Java经典编程题50道之三十七

    有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位. public class Example37 { public static v ...

  6. Java经典编程题50道之三十五

    有一个数组,将其最大的元素与第一个元素交换,最小的元素与最后一个元素交换,然后输出数组. public class Example35 {    public static void main(Str ...

  7. Java经典编程题50道之三十四

    输入3个数a,b,c,按大小顺序输出. public class Example34 {    public static void main(String[] args) {        sort ...

  8. Java经典编程题50道之三十二

    取一个整数a从右端开始的4-7位. public class Example32 {    public static void main(String[] args) {        cut(12 ...

  9. Java经典编程题50道之三十一

    将一个数组逆序输出. public class Example31 {    public static void main(String[] args) {        int[] a = { 9 ...

随机推荐

  1. 2.Ubuntu16.04安装QT5.8.0

    VSCode编辑器开发CPP:http://www.cnblogs.com/dotnetcrazy/p/6661921.html 下载QT run文件(安装包),一般都是这两个下载的比较多,我这边使用 ...

  2. kvc模式详解

    java利用反射机制访问类的 私有变量.OC的KVC模式也是一样,用来访问私有变量用的. 用字符串去访问对象实例变量的机制. //KVC模式-(void)KVCtest{ [_dm valueForK ...

  3. 第一个 lua 程序

    第一个 lua 程序 lua 提供一个交互式编程模式, 直接在命令行输入 lua 开启 $ lua > -- 此处可以输入 lua 程序 lua 脚本执行时的 2 种方式 lua + lua 脚 ...

  4. Java Excel 合并单元格

    //合并单元格CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 4);sheet.addMergedRegion(cra);

  5. C语言学习第五章

    今天要进行一个重要元素数组的学习了.这一章要掌握什么是数组,数组怎么样命名,数组怎么样使用,以及一些常见的错误和需要注意的事项. 一.      数组的基本概念 数组是可以在内存中连续存储多个元素的结 ...

  6. linux编译安装php7

    1.首先下载php7 使用wget命令下载 wget http://cn2.php.net/distributions/php-7.0.12.tar.bz2 2.然后解压 tar -xvf php-7 ...

  7. JS设计模式---缓存代理

    缓存代理可以为一些开销大的运算结果提供暂时的存储,在下次运算的时候,传进来的参数跟上次是一致, 则可以直接返回前面存储的结果. 运行上面的代码我们发现,当第二次再调用proxyMult(1,2,3)的 ...

  8. js图片轮播

    html部分 <a href="" id="pta"><div id="picture" class="pt&q ...

  9. Spring+SpringMvc+Mybatis 框架的搭建(一)

    本文是因为实习结束后学习到了新的技术,想写下来和更多人交流.开发中遇到的问题我也会一一说明,希望有更多人可以互相探讨,加入到一起来. 1. Spring+SpringMvc +Mybatis 的作用有 ...

  10. Jdk1.6 JUC源码解析(13)-LinkedBlockingQueue

    功能简介: LinkedBlockingQueue是一种基于单向链表实现的有界的(可选的,不指定默认int最大值)阻塞队列.队列中的元素遵循先入先出 (FIFO)的规则.新元素插入到队列的尾部,从队列 ...