强化训练_day10

目录

day07_0424

选择题

编程 

day08_0426

选择题

编程

day09_0427

选择题

编程 

day10_0428

选择题

编程


day07_0424

选择题

 

 hasNext()为Iterator的方法,表示迭代时是否有下一个元素

接口只能由类来实现不能被类继承,类可以实现多个接口没有限制,但是类只能继承一个父类,不能同时继承多个类,接口可以继承多个接口。如果你问的是一个类能不能继承一个类同时实现一个接口,那是可以的。如果你说的是一个类和两个接口的关系,那么一个类似可以同时实现两个接口的。

round()方法可以这样理解:四舍五入取值

将括号内的数+0.5之后,向下取值

 

this代表的是当前类的实例对象,可以在类的实例化方法中作为对象来使用,不能用来修饰类或者方法。 

编程 

例如输入15

        输出2

import java.util.Scanner;

/**Fibonacci数列
 * @editor biubiubiu
 */
public class Fibonacci {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        System.out.println(step(x));
    }

    private static int step(int x) {
        if (x==1){
            return 0;
        }
        int step=0;
        int p=0;
        int q=1;
        while (true){
            p=p+q;
            if (p>=x){
               step=Math.min(p-x,x-q);
               break;
            }
            q=p+q;
            if(q>=x){
                step=Math.min(q-x,x-p);
                break;
            }
        }
        return step;
    }
}

 

public class Parenthesis {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String A=sc.nextLine();
        int n=sc.nextInt();
        System.out.println(chkParenthesis(A,n));
    }
    public static boolean chkParenthesis(String A, int n) {
        if (A.length()==0){
            return true;
        }
        if (A.length()!=n){
            return false;
        }
        if (A.charAt(0)!='('){
            return false;
        }
        boolean ischk=false;
        Stack<Character> stack=new Stack<>();
        stack.push(A.charAt(0));
        for (int i = 1; i < A.length(); i++) {
            if(A.charAt(i)!='('&&A.charAt(i)!=')'){
                return false;
            }
            if(A.charAt(i)==')'){
                if (stack.isEmpty()){
                    return false;
                }
               char cur=stack.pop();
               if (cur!='('){
                   stack.push(cur);
                   stack.push(A.charAt(i));
               }
            }else{
                stack.push(A.charAt(i));
            }
        }
          if (stack.isEmpty()){
              ischk=true;
          }
          return ischk;
    }
}

这题不严谨

day08_0426

选择题



JUnit 是一个 Java 语言的单元测试框架。它由 Kent Beck 和 Erich Gamma 建立,逐渐成为源于 Kent Beck 的 sUnit 的 xUnit 家族中最为成功的一个。 JUnit 有它自己的 JUnit 扩展生态圈。多数 Java 的开发环境都已经集成了 JUnit 作为 单元测试的工具 。 

java的GC回收是完全自动的,没有提供相关api手动回收,所有的内存分配和回收权限都在jvm,在开发人员手里没有绝对的强制垃圾回收的方法,不过可以这样去做:
1. 对于不再引用的对象,及时把它的引用赋为null。 obj = null;
2. 如果内存确实很紧张,调用System.gc() 方法来建议垃圾回收器开始回收垃圾,通知GC运行,但是Java语言规范并不保证GC一定会执行
 

编程

两种排序方法

public class Koala {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String[] s=new String[n];
        for (int i = 0; i < n; i++) {
            s[i]=sc.next();
        }
        boolean l=true;
        boolean w=true;
        for (int i = 1; i < n; i++) {
            String str1=s[i-1];
            String str2=s[i];
            //判断符合长度排列
            if (str1.length()>str2.length()){
               //判断符合字典序排列
                l=false;
            }
            if (!isword(str1,str2)){
                w=false;
            }

        }
        if(l){
            if(w){
                System.out.println("both");
            }else {
                System.out.println("lengths");
            }
        }else {
            if (w){
                System.out.println("lexicographically");
            }else {
                System.out.println("none");
            }
        }
    }

    private static boolean isword(String str1, String str2) {
       if (str1.compareTo(str2) <=0){
           return true;
       }
    
        return false;
    }

}

用compareTo()比较两个字符串的nei'o'r

求最小公倍数

public class MinCommon {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println(minNum(a,b));
    }
    private static int minNum(int a, int b) {
        if (a==1&&b==1){
            return 1;
        }
        int max=Math.max(a,b);
        int min=Math.min(a,b);
        if(max%min==0){
           return max;
        }
        int i=2;
        int m=max;
        while(m%min!=0){
            m=i*max;
            i++;
        }
        return m;
    }
}

day09_0427

选择题

数组是对象,所有的对象都储存在堆内存中,而基本数据类型存储在栈内存中

toUpperCase():表示将字符串中的字母全部转化为大写状态。但不会改变原始字符串

replace():表示将用一个字符串替换字符串中的所出现的特定内容。

1. 子类的构造方法总是先调用父类的构造方法,如果子类的构造方法没有明显地指明使用父类的哪个构造方法,子类就调用父类不带参数的构造方法。
2. 而父类没有无参的构造函数,所以子类需要在自己的构造函数中显示的调用父类的构造函数。
3. 解决办法是在构造函数第一行使用super关键字
 

 覆盖和重载的区别:

1、方法的覆盖是子类和父类之间的关系,是垂直关系;方法的重载是同一个类中方法之间的关系,是水平关系。
2、覆盖只能由一个方法,或只能由一对方法产生关系;方法的重载是多个方法之间的关系。
3、覆盖要求参数列表相同;重载要求参数列表不同。
4、覆盖关系中,调用那个方法体,是根据对象的类型(对象对应存储空间类型)来决定;重载关系,是根据调用时的实参表与形参表来选择方法体的。


接口中的属性,都是静态常量,默认修饰符public static final
接口中的方法,默认修饰符public abstract。JDK8以后,引入了static方法和default方法。

基本类型无法调用equals()

 

 复制的效率:System.arraycopy>clone>Arrays.copyOf>for循环

  • System.arraycopy():native方法+JVM手写函数,在JVM里预写好速度最快
  • clone():native方法,但并未手写,需要JNI转换,速度其次
  • Arrays.copyof():本质是调用1的方法
  • for():全是深复制,并且不是封装方法,最慢情有可原编程

编程 

走方格方案数
import java.util.Scanner;

/**
 * @editor biubiubiu
 */
public class PlanSum {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[][] arr=new int[n+1][m+1];
        for (int i = 0; i <=n; i++) {
            for (int j = 0; j <= m; j++) {
                if(i==0||j==0){
                    arr[i][j]=1;
                }else {
                    arr[i][j]=arr[i][j-1]+arr[i-1][j];
                }
            }
        }

        System.out.println(arr[n][m]);
    }

}

 类似与这样的算法但此题是按边缘线走,应该多出一行一列。

day10_0428

选择题

b4,b5被声明final所以类型是不会转换的

b1+b2被转换成int类型 

算术异常类:ArithmeticExecption
空指针异常类:NullPointerException
类型强制转换异常:ClassCastException
数组负下标异常:NegativeArrayException
数组下标越界异常:ArrayIndexOutOfBoundsException
违背安全原则异常:SecturityException
文件已结束异常:EOFException
文件未找到异常:FileNotFoundException
字符串转换为数字异常:NumberFormatException
操作数据库异常:SQLException
输入输出异常:IOException
方法未找到异常:NoSuchMethodException

 a属于类所以在堆中。bc属于方法,b c为局部变量,局部变量不属于任何类或者实例,因此它总是保存在其所在方法的栈内存中!

编程

井字棋

public class Board {
    public boolean checkWon(int[][] board) {
        boolean flag = false;
        for (int i = 0; i < 3; i++) {
            int n = 0;
            for (int j = 0; j < 3; j++) {
                n += board[i][j];
            }
            if (n == 3) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            for (int j = 0; j < 3; j++) {
                int n = 0;
                for (int i = 0; i < 3; i++) {
                    n += board[i][j];
                }
                if (n == 3) {
                    flag = true;
                }
            }
        }
        if (!flag){
            int i=board[0][0]+board[1][1]+board[2][2];
            int j=board[0][2]+board[1][1]+board[2][0];
            if(i==3||j==3){
                flag=true;
            }
        }
        return flag;
    }
}

 密码强度等级

public class PassWord {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String  password=sc.next();
        rank(password);
    }
    public static void rank(String str){
        int grade=0;
        //密码长度
       if (str.length()<=4){
           grade+=5;
       }else {
           if (str.length()>4&&str.length()<8){
               grade+=10;
           }else {
               grade+=25;
           }
       }
       //字母
        int a=0;//记录有几个小写字母
       int A=0;//记录有几个大写字母
        for (int i = 0; i <str.length() ; i++) {
            if (str.charAt(i)>='A'&&str.charAt(i)<='Z'){
                A++;
                }else {
                if (str.charAt(i)>='a'&&str.charAt(i)<='z'){
                    a++;
                }
            }
        }
       if(A!=0&&a!=0){
           grade+=20;
       }else {
           if(A!=0||a!=0){
               grade+=10;
           }
       }
       //数字
        int n=0;//记录有几个数字
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
                n++;
            }
        }
       if(n==1){
           grade+=10;
       }else {
           if(n>1){
               grade+=20;
           }
       }
       //符号
        int f=0;//记录有机会个字符
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if(c>='!'&&c<='/'){
                f++;
            }else{
                if(c>=':'&&c<='@'){
                    f++;
                }else {
                    if(c>='['&&c<='`'){
                        f++;
                    }else {
                        if (c>='{'&&c<='~'){
                            f++;
                        }
                    }
                }
            }
        }
        if(f==1){
            grade+=10;
        }else {
            if (f>1){
                grade+=25;
            }
        }
       //奖励
       if (A!=0&&a!=0&&n!=0&&f!=0){
           grade+=5;//密码有大小写字母,数字,符号
       }else {
           if((A!=0||a!=0)&&n!=0&&f!=0){
               grade+=3;//密码有字母,数字,符号
           }else {
               if((A!=0||a!=0)&&n!=0){
                   grade+=2;//字母,数字
               }
           }
       }
       //评分
        if(grade>=95){
            System.out.println("VERY_SECURE");
        }else {
            if (grade>=80){
                System.out.println("SECURE");
            }else {
                if (grade>=70){
                    System.out.println("VERY_STRONG");
                }else {
                    if (grade>=60){
                        System.out.println("STRONG");
                    }else {
                        if (grade>=50){
                            System.out.println("AVERAGE");
                        }else {
                            if (grade>=25){
                                System.out.println("WEAK");
                            }else {
                                System.out.println("VERY_WEAK");
                            }
                        }
                    }
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值