【笔试题】简单的两道笔试题(1、打印杨辉三角;2、三个数排序)

笔试题 简单的两道笔试题(1、打印杨辉三角;2、三个数排序)
1、打印杨辉三角
1306719-20180813170200566-892950193.png

import java.util.Scanner;
public class MyYanghuiTriangle {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int row = scan.nextInt();
        int[][] nums = new int[row][];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = new int[i + 1];
            for (int s = 0; s < nums.length - 1 - i; s++) {
                System.out.print("  ");
            }
            for (int j = 0; j < nums[i].length; j++) {
                if (j == 0 || j == nums[i].length - 1) {
                    nums[i][j] = 1;
                } else {
                    nums[i][j] = nums[i-1][j] + nums[i - 1][j - 1];
                }
                System.out.printf("%4d", nums[i][j]);
            }
            System.out.println();
        }
        scan.close();
    }
}

1306719-20190414210726169-1621891391.png

2、三个数排序(输入3个数a,b,c,按大小顺序输出。)

import java.util.Scanner;
public class ThreeSort {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int c = scan.nextInt();
        if(a<b) {
            int temp = a;
            a = b;
            b = temp;
        }
        if(a<c) {
            int temp = a;
            a = c;
            c = temp;
        }
        if(b<c) {
            int temp = b;
            b = c;
            c = temp;
        }
        scan.close();
        System.out.print("从大到小的顺序输出:");
        System.out.println(a+" "+b+" "+c);
    }
}

1306719-20190414210729938-841906564.png

转载于:https://www.cnblogs.com/hglibin/p/9469404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值