zoukankan      html  css  js  c++  java
  • java之数学方法

    参考http://how2j.cn/k/number-string/number-string-math/319.html

    java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在

    四舍五入, 随机数,开方,次方,π,自然常数

    package digit;
      
    public class TestNumber {
      
        public static void main(String[] args) {
            float f1 = 5.4f;
            float f2 = 5.5f;
            //5.4四舍五入即5
            System.out.println(Math.round(f1));
            //5.5四舍五入即6
            System.out.println(Math.round(f2));
             
            //得到一个0-1之间的随机浮点数(取不到1)
            System.out.println(Math.random());
             
            //得到一个0-10之间的随机整数 (取不到10)
            System.out.println((int)( Math.random()*10));
            //开方;先对9开方,然后转成int类型
            System.out.println(Math.sqrt(9));
         //例如,(int)Math.sqrt(2)=(int)1.414=1
    //次方(2的4次方) System.out.println(Math.pow(2,4)); //π System.out.println(Math.PI); //自然常数 System.out.println(Math.E);
    
    

          Math.abs();取绝对值

          Math.floor();向上取整

          Math.ceil();向下取整
    
        }
    }

    练习-质数

     找到1-1000中的质数,调优后的算法

    private static ArrayList<Integer> getZhiShu1(){
            ArrayList<Integer> list=new ArrayList<>();
            list.add(2);
            for (int i=3;i<1000;i+=2){
                boolean b=true;
                for (int j=3;j<=Math.sqrt(i);j+=2){
                    if(i%j==0){
                        b=false;
                    }
                }
                if(b==true){
                    list.add(i);
                }
            }
            return list;
        }
  • 相关阅读:
    PHP 语法
    PHP 变量
    为什么说PHP是个集中营
    简单介绍ThinkPHP3.1.3使用笔记
    PHP实现提交表单及输出例子
    linux 用户组以及权限
    linux vim学习
    linux基础指令学习
    pycharm
    Codeforces Round #346 (Div. 2) C题
  • 原文地址:https://www.cnblogs.com/lijingran/p/9127405.html
Copyright © 2011-2022 走看看