刷题笔记之四(Fibonacci数列+合法括号序列判断+跳石板+幸运的袋子+两种排序方式+最小公倍数)

目录

android-studio

1. Math类是封装了常用的数学运算

Json转Model实现分析

2. Object类的12种常用方法

API

3. Fibonacci数列

piexif的使用

4. 合法括号序列判断

ofdma

5. 子类父类trycatch调用

矩阵

6. 跳石板

ConfigurationProperties

7. 幸运的袋子

模型训练

8.跳出forEach循环break

RK3399

9 .java为后缀的文件中,只能有一个public修饰并且文件名相同的类

MMCM

10. a++先使用后++

JavaSE

11. 两种排序方式

强化学习

12. 最小公倍数

一条SQL语句是如何执行的


热更新

1. Math类是封装了常用的数学运算

电磁寻迹

在Java中Math类封装了常用的数学运算,Math位于java.lang包。它的构造方法是private的,所以无法创建Math类的对象,并且Math类的所有方法都是类方法,可以直接通过类名来调用它们。

IDE安装

Math.abs(Type number) 取绝对值,返回值是传入的类型 Math.cell(double number) 返回值是double
Math.floor(double number) 向下取整,返回值是double Math.round(Type number) 四舍五入
Math.random() 取一个大于等于0.0,小于等于1.0的随机数 Math.max(Type x,Type y) 取最大值,返回值是double
Math.min(Type x,Type y) 取最小值,返回值是double Math.sqrt(double number) 计算平方根

2021版C语言教程

魔法方法

framework

iOS计算器微信小程序

rk3588

hadoop

token


2. Object类的12种常用方法

ios证书

Object类是所有类的祖先,在jdk1.8中Object一共有12种方法

动画延时重复播放

v3

 根据用途可以分为6种

javascript

1.构造函数

es

2.hashCode和equals函数用来判断对象是否相同

缓冲流

3.wait() , wait(long) , wait(long , int) , notify() , notifyAll() 主要在多线程中用

list

4.toString()和getClass都是返回对象,toString返回String对象,getClass返回class对象

5.clone()用来另存一个当前存在的对象

6.finalize()用于垃圾回收


3. Fibonacci数列

题目链接:Fibonacci数列_牛客题霸_牛客网 (nowcoder.com)

题目要求:

题目分析:

 上代码

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int f1 = 0;
            int f2 = 1;

            while(f2 < n) {
                int f3 = f1 + f2;
                f1 = f2;
                f2 = f3;  
            }
            //循环结束 f1 < N <= f2
            int min = Math.min(n-f1,f2-n);
            System.out.println(min);
    }
}


4. 合法括号序列判断

题目链接:合法括号序列判断_牛客题霸_牛客网 (nowcoder.com)

题目要求:

 题目分析:

 上代码

public boolean chkParenthesis(String A, int n) {
        if(A.length() % 2 != 0) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (char c : A.toCharArray()) {
            if(c == '(') {
                stack.push(c);
            }else if(c == ')') {
                if(stack.isEmpty()) {
                    return false;
                }else {
                    stack.pop();
                }
            }else {
                return false;
            }
        }
       return stack.isEmpty();
    }

5. 子类父类trycatch调用

6. 跳石板

题目链接:跳石板_牛客题霸_牛客网 (nowcoder.com)

题目要求:

题目分析:

 上代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[] step = new int[m+1];
        for (int i = 0; i < m+1; i++) {
            step[i] = Integer.MAX_VALUE;
        }
        step[n] = 0;
        //开始跳,i代表当前石板的编号
        for (int i = n; i < m; i++) {
            if(step[i] == Integer.MAX_VALUE) {
                //无法跳跃到这个位置
                continue;
            }
            List<Integer> list = div(i);
            //j代表一次可以跳几块石板
            for (int j : list) {
                if(i+j <= m && step[i+j] != Integer.MAX_VALUE) {
                    //当前石板本身的次数 和 现在刚过来的次数进行比较
                    step[i+j] = Math.min(step[i+j],step[i]+1);
                }else if(i+j <= m) {
                    //当前石板上是默认的MAX,那刚跳过来就在上一次的基础上+1
                    step[i+j] = step[i]+1;
                }
            }
        }
        if(step[m] == Integer.MAX_VALUE) {
            //当前m上如果为MAX,说明就没有跳过来
            System.out.println(-1);
        }else {
            System.out.println(step[m]);
        }
    }
    //求i的约数
    private static List<Integer> div(int num) {
        List<Integer> list = new ArrayList<>();
        for (int i = 2; i*i <= num; i++) {
            if(num%i == 0) {
                list.add(i);
                //比如16的约数4,上面已经放了4下面就不能重复放
                //这里放大于i的约数
                if(num/i != i) {
                    list.add(num/i);
                }
            }
        }
        return list;
    }
}

7. 幸运的袋子

题目链接:幸运的袋子_牛客题霸_牛客网 (nowcoder.com)

题目要求:

题目分析:

上代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
        int[] array = new int[m];
        for (int i = 0; i < m; i++) {
            array[i] = scan.nextInt();
        }
        Arrays.sort(array);
        System.out.println(count(array,m,0,0,1));
    }

    public static int count(int[] a, int n, int pos, int sum, int multi) {
        int count = 0;
        for (int i = pos; i < n; i++) {
            sum += a[i];
            multi *= a[i];
            if(sum > multi) {
                count = count + 1 + count(a,n,i+1,sum,multi);
            }else if(a[i] == 1) {
                count = count + count(a,n,i+1,sum,multi);
            }else {
                break;
            }
            sum = sum - a[i];
            multi = multi/a[i];
            //拥有相同号码的球是无区别的,判断下一个球和当前是否一样
            while(i < n-1 && a[i] == a[i+1]) {
                i++;
            }
        }
        return count;
    }
}

8.跳出forEach循环break

在方法中,返回值可能为int String boolean 等,而B,C中返回一个boolean类型的,可能不是当前方法所要的返回值

而想要跳出循环,break就可以了

9 .java为后缀的文件中,只能有一个public修饰并且文件名相同的类

 以.java为后缀的文件中,可以包含多个类,但只能有一个public修饰并且文件名相同的类


10. a++先使用后++

 


11. 两种排序方式

题目链接:两种排序方法_牛客题霸_牛客网 (nowcoder.com)

题目要求:

 题目分析:

这道题是两种排序方式:按照字典顺序排列单词;按照单词长度排列单词

综合根据两种排序方式,输出对应语句

所以可以考虑将两张排序方式,单独写两个方法

isSortZidian():如果给方法中传的参数是字符串数组的话,那么比较时直接在循环里用comparTo()比较两个单词就可以

isSortLength():如果给方法中传的参数是字符串数组的话,那么比较时直接在循环里比较两个字符串的长度就可以了

所以我们最好在前面输入时,将输入的每一行字符放进字符串数组中去

也就是每一行读取,放入字符串数组中,这里可以考虑使用BufferedReader

BufferedReader是从缓冲区之中读取内容,所有的输入的字节数据都将放在缓冲区之中。

主要是因为BufferedReader中有一个方法readLine(),使用起来特别方便,每次读回来的都是一行,但System.in本身表示的是InputSteam(字节流),现在要求接收的是一个字符流,这里就需要将字节流转为字符流就可以了用InputStreamReader

  BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));

上代码

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
        //每次读一行 readLine
        int n = Integer.parseInt(bi.readLine());
        String[] str = new String[n];
        for(int i = 0; i < n; i++) {
            str[i] = bi.readLine();
        }
        //判断
         if(isSortZidian(str) && !isSortLength(str)) {
            System.out.println("lexicographically");
        }else if(!isSortZidian(str) && isSortLength(str)) {
            System.out.println("lengths");
        }else if(isSortZidian(str) && isSortLength(str)) {
            System.out.println("both");
        }else {
            System.out.println("none");
        }
    }

    private static boolean isSortZidian(String[] str) {
        for(int i = 0; i < str.length-1; i++) {
            if(str[i].compareTo(str[i+1]) > 0) {
                return false;
            }
        }
        return true;
    } 
     private static boolean isSortLength(String[] str) {
        for (int i = 0; i < str.length-1; i++) {
            if(str[i].length() > str[i+1].length()) {
                return false;
            }
        }
        return true;
    }
}

12. 最小公倍数

题目链接:求最小公倍数_牛客题霸_牛客网 (nowcoder.com)

题目要求:

题目分析:

 AB最小公倍数 = A*B / (AB最大公约数)

AB最大公约数用辗转相除法

记住这个公式,下次写辗转相除法照着这个写 gcd(a,b)=gcd(b,a%b) 

还是感觉递归的这个对照这个公式好写

上代码

import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        System.out.println(n*m/gcd(n,m));
    }

    //辗转相除法求最大公约数
    private static int gcd(int a, int b ) {
        if(b == 0){
            return a;
        }
        int r = a%b;
        return gcd(b,r);
    }
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注