zoukankan      html  css  js  c++  java
  • 学习笔记2

    +=,-=,*=,/=隐含了强制类型转换。

    逻辑运算中,&和&&的区别为:

             &:无论左边为真假,右边都参与运算

             &&:如果左边为假,则右边不参与运算,如果左边为真,右边参与运算。

    异或操作可以实现两个整数的交换(不需要额外空间开销):

             a, b;

             a = a ^ b;

             b = a ^ b;

             a = a ^ b;

    实现两个整数交换的4种常用方法:

    1)  引入第3个变量temp

    2)  使用异或操作符^

    3)  利用加减法运算

    a = a + b;

    b = a – b;

    a = a – b;

    4)  一条搞定

    b = (a + b) – (a = b);

    用二维数组保存并输出杨辉三角形(不同的行的数据个数不同):

    /**
    *
    */
    package com.yan.javaTest;
    import java.util.Scanner;

    /**
    * @author Yan
    *
    */
    public class YangHuiSanJiao {

    /**
    * Yang hui san jiao xing.
    *
    * 1
    * 1 1
    * 1 2 1
    * 1 3 3 1
    * 1 4 6 4 1
    * 1 5 10 10 5 1
    *
    * 1. Mei hang de di yi lie he zui hou yi lie dou shi 1;
    * 2. Cong di san hang kai shi zhongjianlie de shuzhi wei shangyihang de qianyilie he dangqianlie
    * shuzhi zhihe.
    *
    */
    public YangHuiSanJiao() {
    // TODO Auto-generated constructor stub
    }
    /**
    * @param args
    */
    // 数组赋值
    int[][] yangHuiSanJiaoXing(int[][] str){
    for(int i=2;i<str.length;i++){ //from the third line str[2][];
    for(int j=1;j<str[i].length-1;j++){ //from str[i][1] ---- str[i][length-2];
    str[i][j]=str[i-1][j-1]+str[i-1][j];
    }
    }
    return str;
    }
    // 数组格式初始化
    int[][] arrInit(int[][] str,int n){
    for(int i=0;i<n;i++){
    str[i]=new int[i+1];
    str[i][0]=1;
    str[i][i]=1;
    }
    return str;
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Please input a number to assign the lines of the triangle:");
    Scanner sc = new Scanner(System.in);
    int n=sc.nextInt();
    int[][] arr=new int[n][];
    YangHuiSanJiao ys = new YangHuiSanJiao();
    arr=ys.arrInit(arr, n);
    arr=ys.yangHuiSanJiaoXing(arr);
    for(int i=0;i<arr.length;i++){
    for(int j=0;j<arr[i].length;j++){
    System.out.print(arr[i][j]+" ");
    }
    System.out.println();
    }
    sc.close();
    }

    }

  • 相关阅读:
    如何基于Azure平台实现MySQL HA(方法论篇)
    如何对Azure磁盘性能进行测试
    Azure底层架构的初步分析
    聊聊Azure的安全性
    关于Azure带宽的测试
    JavaScript 优化
    SQL时间段查询
    win7+64位+Oracle+11g+64位下使用PLSQL+Developer+的解决办法
    putty 使用方法,中文教程
    怎样才能专心工作
  • 原文地址:https://www.cnblogs.com/yanspecial/p/5185664.html
Copyright © 2011-2022 走看看