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


    目录

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

    2. Object类的12种常用方法

    3. Fibonacci数列

    4. 合法括号序列判断

    5. 子类父类trycatch调用

    6. 跳石板

    7. 幸运的袋子

    8.跳出forEach循环break

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

    10. a++先使用后++

    11. 两种排序方式

    12. 最小公倍数


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

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

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


    2. Object类的12种常用方法

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

     根据用途可以分为6种

    1.构造函数

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

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

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

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

    6.finalize()用于垃圾回收


    3. Fibonacci数列

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

    题目要求:

    题目分析:

     上代码

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner in = new Scanner(System.in);
    5. int n = in.nextInt();
    6. int f1 = 0;
    7. int f2 = 1;
    8. while(f2 < n) {
    9. int f3 = f1 + f2;
    10. f1 = f2;
    11. f2 = f3;
    12. }
    13. //循环结束 f1 < N <= f2
    14. int min = Math.min(n-f1,f2-n);
    15. System.out.println(min);
    16. }
    17. }


    4. 合法括号序列判断

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

    题目要求:

     题目分析:

     上代码

    1. public boolean chkParenthesis(String A, int n) {
    2. if(A.length() % 2 != 0) {
    3. return false;
    4. }
    5. Stack stack = new Stack<>();
    6. for (char c : A.toCharArray()) {
    7. if(c == '(') {
    8. stack.push(c);
    9. }else if(c == ')') {
    10. if(stack.isEmpty()) {
    11. return false;
    12. }else {
    13. stack.pop();
    14. }
    15. }else {
    16. return false;
    17. }
    18. }
    19. return stack.isEmpty();
    20. }

    5. 子类父类trycatch调用

    6. 跳石板

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

    题目要求:

    题目分析:

     上代码

    1. import java.util.ArrayList;
    2. import java.util.List;
    3. import java.util.Scanner;
    4. public class Main {
    5. public static void main(String[] args) {
    6. Scanner scan = new Scanner(System.in);
    7. int n = scan.nextInt();
    8. int m = scan.nextInt();
    9. int[] step = new int[m+1];
    10. for (int i = 0; i < m+1; i++) {
    11. step[i] = Integer.MAX_VALUE;
    12. }
    13. step[n] = 0;
    14. //开始跳,i代表当前石板的编号
    15. for (int i = n; i < m; i++) {
    16. if(step[i] == Integer.MAX_VALUE) {
    17. //无法跳跃到这个位置
    18. continue;
    19. }
    20. List list = div(i);
    21. //j代表一次可以跳几块石板
    22. for (int j : list) {
    23. if(i+j <= m && step[i+j] != Integer.MAX_VALUE) {
    24. //当前石板本身的次数 和 现在刚过来的次数进行比较
    25. step[i+j] = Math.min(step[i+j],step[i]+1);
    26. }else if(i+j <= m) {
    27. //当前石板上是默认的MAX,那刚跳过来就在上一次的基础上+1
    28. step[i+j] = step[i]+1;
    29. }
    30. }
    31. }
    32. if(step[m] == Integer.MAX_VALUE) {
    33. //当前m上如果为MAX,说明就没有跳过来
    34. System.out.println(-1);
    35. }else {
    36. System.out.println(step[m]);
    37. }
    38. }
    39. //求i的约数
    40. private static List div(int num) {
    41. List list = new ArrayList<>();
    42. for (int i = 2; i*i <= num; i++) {
    43. if(num%i == 0) {
    44. list.add(i);
    45. //比如16的约数4,上面已经放了4下面就不能重复放
    46. //这里放大于i的约数
    47. if(num/i != i) {
    48. list.add(num/i);
    49. }
    50. }
    51. }
    52. return list;
    53. }
    54. }

    7. 幸运的袋子

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

    题目要求:

    题目分析:

    上代码

    1. import java.util.Arrays;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scan = new Scanner(System.in);
    6. int m = scan.nextInt();
    7. int[] array = new int[m];
    8. for (int i = 0; i < m; i++) {
    9. array[i] = scan.nextInt();
    10. }
    11. Arrays.sort(array);
    12. System.out.println(count(array,m,0,0,1));
    13. }
    14. public static int count(int[] a, int n, int pos, int sum, int multi) {
    15. int count = 0;
    16. for (int i = pos; i < n; i++) {
    17. sum += a[i];
    18. multi *= a[i];
    19. if(sum > multi) {
    20. count = count + 1 + count(a,n,i+1,sum,multi);
    21. }else if(a[i] == 1) {
    22. count = count + count(a,n,i+1,sum,multi);
    23. }else {
    24. break;
    25. }
    26. sum = sum - a[i];
    27. multi = multi/a[i];
    28. //拥有相同号码的球是无区别的,判断下一个球和当前是否一样
    29. while(i < n-1 && a[i] == a[i+1]) {
    30. i++;
    31. }
    32. }
    33. return count;
    34. }
    35. }

    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));

    上代码

    1. import java.util.Scanner;
    2. import java.io.*;
    3. public class Main {
    4. public static void main(String[] args) throws IOException{
    5. BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
    6. //每次读一行 readLine
    7. int n = Integer.parseInt(bi.readLine());
    8. String[] str = new String[n];
    9. for(int i = 0; i < n; i++) {
    10. str[i] = bi.readLine();
    11. }
    12. //判断
    13. if(isSortZidian(str) && !isSortLength(str)) {
    14. System.out.println("lexicographically");
    15. }else if(!isSortZidian(str) && isSortLength(str)) {
    16. System.out.println("lengths");
    17. }else if(isSortZidian(str) && isSortLength(str)) {
    18. System.out.println("both");
    19. }else {
    20. System.out.println("none");
    21. }
    22. }
    23. private static boolean isSortZidian(String[] str) {
    24. for(int i = 0; i < str.length-1; i++) {
    25. if(str[i].compareTo(str[i+1]) > 0) {
    26. return false;
    27. }
    28. }
    29. return true;
    30. }
    31. private static boolean isSortLength(String[] str) {
    32. for (int i = 0; i < str.length-1; i++) {
    33. if(str[i].length() > str[i+1].length()) {
    34. return false;
    35. }
    36. }
    37. return true;
    38. }
    39. }

    12. 最小公倍数

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

    题目要求:

    题目分析:

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

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

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

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

    上代码

    1. import java.util.Locale;
    2. import java.util.Scanner;
    3. public class Main {
    4. public static void main(String[] args) {
    5. Scanner scan = new Scanner(System.in);
    6. int n = scan.nextInt();
    7. int m = scan.nextInt();
    8. System.out.println(n*m/gcd(n,m));
    9. }
    10. //辗转相除法求最大公约数
    11. private static int gcd(int a, int b ) {
    12. if(b == 0){
    13. return a;
    14. }
    15. int r = a%b;
    16. return gcd(b,r);
    17. }
    18. }

     

  • 相关阅读:
    自定义MVC原理
    SSH免密登陆 配置方法(Mac、Win)
    什么是Node.js的NVM(Node Version Manager)?它的作用是什么?
    【Redis】对象
    片内总线在cpu扮演什么角色?他为什么能实现高效,不同的CPU为什么采用不同的总线协议?
    Tensor 的广播机制
    用面向对象的方式操作 JSON 甚至还能做四则运算 JSON 库
    Docker 镜像管理
    用于光波导系统的均匀性探测器
    WebSocket 和 Socket 的区别
  • 原文地址:https://blog.csdn.net/m0_58761900/article/details/127461519