import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
//        new Case019_100().test();

//        new Case027().test();
//        new Case034().test();
       new Case037().test();

//        new Case036().test();
//        new Case042().test();
    }
}

class Case019_100 {
    public void test() {
//        dispose019();
//        dispose020();
//        dispose021();
//        dispose022();
//        dispose023();
//        dispose026();
//        dispose034();
    }

    private void dispose019() {
        System.out.println("Out");
        System.err.println("Err");
    }
    private void dispose020() {
        System.out.println("Your Input:");
        String line = new Scanner(System.in).nextLine();
        System.out.println("YOur Input:"+line);
    }
    private void dispose021() {
        try {
//            PrintStream out = new PrintStream(new FileOutputStream("./src/stdout"));
            PrintStream out = new PrintStream("./src/stdout"); //这个地方可以优化
            System.setOut(out);
            System.out.println("nihao");
            System.out.println("happy to see you.");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    private void dispose022() {
        System.out.println("Input A Line:");
        String line = new Scanner(System.in).nextLine();

        //加密
        char[] arr = line.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            arr[i]=(char)(arr[i]^2000);
        }
        System.out.println("Encrypt Result: "+new String(arr));

        //解密
        for (int i = 0; i < arr.length; i++) {
            arr[i]=(char)(arr[i]^2000);
        }
        System.out.println("Decode Result: "+new String(arr));
    }
    private void dispose023() {
        System.out.println("Your Input:");
        Long num = new Scanner(System.in).nextLong();
        String result = num%2==0?"偶数":"奇数";
        System.out.println("Result: "+result);
    }
    private void dispose026() {
        //
        System.out.println("2*16:" + (1<<5));
    }
    private void dispose034() {
        int layer = 10;
        int[][] arrs = new int[layer][layer];

        for (int i = 0; i < layer; i++) {
            for (int j = 0; j < layer; j++) {
                arrs[i][j]=-1;
            }
        }

        for(int i=0;i<layer;i++){

        }
    }
    private void dispose037() {
        int size = 10;
        if (size % 2 == 0) {
            size++;
        }

        int length = size/2+1;
        boolean[][] arrs = new boolean[length][length];

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                if(i==j)
                    arrs[i][j]=true;
                else
                    arrs[i][j]=false;
            }
        }

    }
}

/*
    需求:利用异或运算,完成不借助第三个变量实现两个数字的交换
    核心公式:
        A = A ^ B;
        B = B ^ A;
        A = A ^ B;
    解析:
        A = a; B = b;
        A = a^b;
        B = b^(a^b)=a;
        A = (a^b)^(b^(a^b))=b;
 */
class Case027 {
    public void test() {

        dispose(10,20);
    }

    private void dispose(int a, int b){
        System.out.println("a: "+a+" b: "+b);

        a=a^b;
        b=a^b;
        a=a^b;

        System.out.println("a: "+a+" b: "+b);
    }
}

/*
    需求:杨辉三角
 */
class Case034 {
    public void test() {
        dispose(10);
    }

    private void dispose(int layer){

        int[][] arrs = new int[layer][layer+2];

        //初始化二维数组
        for (int i = 0; i < layer; i++) {
            for (int j = 0; j < layer + 2; j++) {
                arrs[i][j]=-1;
            }
        }

        //
        for (int i = 0; i < layer; i++) {
            if (i == 0) {
                arrs[i][1]=1;
                arrs[i][0]=0;
                arrs[i][2]=0;
                continue;
            }

            int j;
            for (j = 1; j <= i + 1; j++) {
                arrs[i][j]=arrs[i-1][j]+arrs[i-1][j-1];
            }
            arrs[i][0]=0;
            arrs[i][j]=0;
        }

        for(int m = 0;m<layer;m++){
            for (int n = 1; n <= m+1; n++) {
                System.out.printf("%4d",arrs[m][n]);
            }
            System.out.println();
        }

    }
}

/*
    需求:计算1+1/2!+1/3!+...+1/20!
 */
class Case036 {
    public void test() {
        dispose(20);
        dispose2(20);
    }

    private void dispose(int n) {
        int[] arrs = new int[n+1];
        for (int i = 1; i <= n; i++) {
            if (i == 1) {
                arrs[i]=1;
                continue;
            }

            arrs[i]=arrs[i-1]*i;
        }

        double result = 0.0;
        for (int i = 1; i <= n; i++) {
            result += (double)1/(double)arrs[i];
        }

        System.out.println("Result:"+result);
    }

    //这种写法更精炼
    private void dispose2(int n) {
        BigDecimal sum = new BigDecimal(0.0);
        BigDecimal factorial = new BigDecimal(1.0);

        int i = 1;
        while (i <= n) {
            factorial = factorial.multiply(new BigDecimal(1.0/i));
            sum=sum.add(factorial);
            i++;
        }

        System.out.println("dispose2: "+sum);
    }
}

/*
    需求:空心菱形
 */
class Case037 {
    public void test() {
        dispose(10);
    }

    private void dispose(int size) {
        if (size % 2 == 0) {
            size++;
        }

        for (int i = 1; i <= size; i++) {
            //特殊区域
            if (i == 1 || i == size) {
                for (int j = 1; j <= size / 2; j++) {
                    System.out.print(" ");
                }
                System.out.println("*");
                continue;
            }

            if (i == size / 2 + 1) {
                System.out.print("*");
                for (int j = 2; j < size; j++) {
                    System.out.print(" ");
                }
                System.out.println("*");
                continue;
            }
            //上半部分
            if(i<size/2+1){

                int area = size/2-1;
                int layer = i-1;
                int lx = area-layer+1;
                int rx = layer;

                printEdge(area,lx); //左边
                printEdge(area,rx);//右边
                System.out.println();
            }
            //下半部分
            if(i>size/2+1){

                int area = size/2-1;
                int layer = i-size/2-1;
                int lx = area-layer+1;
                int rx = layer;

                printEdge(area,rx);//左边
                printEdge(area,lx); //右边
                System.out.println();

            }
        }
    }

    private void printEdge(int area, int x) {
        System.out.print(" ");
        for(int i=1;i<=area;i++){
            if (i == x) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
    }
}
/*
    需求:本实例接受用户在文本框中输入的单行数据,其中数据都是整型数字,
    以不同数量的空格分割数字。将这个单行数据分解成一维数组,并从数组中提
    取最小值。
 */
class Case042 {

    public void test() {
        String[] inputs = new String[]{
                "12 23 34 21 23  12  323  2  2 3 ",
                "12 23 34 23 ji 3 21 3  2 32 12 3 2 123"
        };

        for (int i = 0; i < inputs.length; i++) {
            dispose(inputs[i]);
        }
    }

    private void dispose(String str) {
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (!Character.isDigit(c) && !Character.isSpaceChar(c)) {
                System.out.println("该输入有不合法字符");
                return;
            }
        }

        System.out.println("最小的值:"+findMin(split(str)));
    }

    private int[] split(String str) {

        String[] ints = str.split(" +");
        int[] result = new int[ints.length];

        for (int i = 0; i < result.length; i++) {
            result[i]=Integer.valueOf(ints[i]);
        }

        return  result;
    }

    private int findMin(int[] nums) {
        int min = nums[0];

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < min) {
                min = nums[i];
            }
        }

        return min;
    }

}

Java开发实例大全:3月14日练习的更多相关文章

  1. 关于苹果开发证书失效的解决方式(2016年2月14日Failed to locate or generate matching signing assets)

    前言: 从2月14日開始,上传程序的同学可能会遇到提示上传失败的提示. 而且打开自己的钥匙串,发现所有的证书所有都显示此证书签发者无效. Failed to locate or generate ma ...

  2. 关于16年2月14日以后上传AppStore出现:Missing iOS Distribution signing identity for...的问题

    2016年2月14日以后打包上传AppStore会发现出现如下的问题: 导致问题的原因是:下边这个证书过期了 以下是苹果官方给出的回应: Thanks for bringing this to the ...

  3. eoe移动开发者大会—移动开发者的极客盛宴 2013年9月14日期待您的加入!!

    2013 eoe移动开发者大会北京站即将盛大开启!      大会介绍 由国内最大的移动开发者社区eoe主办,在行业盟友的倾力支持下,集合了来自微软.Google.亚马逊.ARM等跨国公司业务负责人的 ...

  4. 湖人VS爵士!!科比4月14日最后一战,本赛季最高得分!狂得60分!!完美大逆转!!!

    莫愁前路无知己,天下谁人不识君.科比,愿你如迈克尔·乔丹,仍然活跃在篮球界.退役不是结束,而是另一段人生的开始. 北京时间2016年4月14日,湖人101-96击败爵士,科比-布莱恩特告别战,20年职 ...

  5. 12月14日《奥威Power-BI销售计划填报》腾讯课堂开课啦

           2016年的最后一个月也过半了,新的一年就要到来,你是否做好了启程的准备?新的一年,有计划,有目标,有方向,才不至于迷茫.规划你的2017,新的一年,遇见更好的自己!        所以 ...

  6. 2016年12月14日 星期三 --出埃及记 Exodus 21:9

    2016年12月14日 星期三 --出埃及记 Exodus 21:9 If he selects her for his son, he must grant her the rights of a ...

  7. 2016年11月14日 星期一 --出埃及记 Exodus 20:5

    2016年11月14日 星期一 --出埃及记 Exodus 20:5 You shall not bow down to them or worship them; for I, the LORD y ...

  8. 2016年10月14日 星期五 --出埃及记 Exodus 18:25

    2016年10月14日 星期五 --出埃及记 Exodus 18:25 He chose capable men from all Israel and made them leaders of th ...

  9. Java Calendar获取年、月、日、时间

    Java Calendar获取年.月.日.时间 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00" ...

随机推荐

  1. 使用Notepad++远程编辑Ubuntu上的源码

    简单搭建了在Windows上远程编辑Ubuntu Server 14.04上面源代码的环境,记录一下,给需要的人. Notepad++安装NppFTP 从插件菜单打开PluginManager,选中N ...

  2. CentOS 7使用yum快速搭建LAMP环境

    1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on # ...

  3. Android零基础入门第82节:Activity数据回传

    上一节学习了将简单的数据从MainActivity传递到SecondActivity,本节一起来学习数据如何从SecondActivity回传到MainActivity. 一.简介 前面己经提到,Ac ...

  4. RIO的性能

    看了一下微软官网RIO没有达到四五倍的宣称(而且必须在windows 2012r2才可以)最多一倍github.com/aspnet/benchmarks测试代码可以从github.com/zelia ...

  5. 一份React-Native学习指南

    直击现场 学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南 本指南汇集Rea ...

  6. Codility---CountFactors

    Task description A positive integer D is a factor of a positive integer N if there exists an integer ...

  7. Oracle高级查询、事物、过程及函数

    一.SQL函数 1.分类:单行函数(日期.数值.转换.字符等),多行函数,也称为分组函数(max.min.avg.sum.row_number.rank等). 2.数值函数 abs(n):求数字n的绝 ...

  8. C语言实现常用排序算法——插入排序

    插入排序是最基础的排序算法,原理: 首先1个元素肯定是有序的,所以插入排序从第二个元素开始遍历:内循环首先请求一个空间保存待插入元素,从当前元素向数组起始位置反向遍历:当发现有大于待插入元素的元素,则 ...

  9. Laravel --- Laravel 5.3 队列使用方法

    一.设置存储方式 在config/queue.php中查看队列驱动,在.env 设置[QUEUE_DRIVER] 主要介绍数据库驱动 二.数据库驱动 1.修改.env CACHE_DRIVER=fil ...

  10. python文件及路径管理函数

    glob模块 说明: 1.glob是python自己带的一个文件操作相关模块,用它可以查找符合自己目的的文件,就类似于Windows下的文件搜索, 支持通配符操作 *.?.[] 这三个通配符,*代表0 ...