Java刷题面试系列习题(二十一)

发布于:2022-11-28 ⋅ 阅读:(702) ⋅ 点赞:(0)

🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
 
🍂个人博客首页: KJ.JK
 
💖系列专栏:Java刷题面试系列
 
💨推荐一款实用的模拟面试、刷题练习算法的神器、适用于所有的程序猿👉点击开始免费刷题,跟着博主走上巅峰💪


前言

博主偶然的一次刷题,发现了这个非常不错的网站牛客网,进去里面刷了一下,发现覆盖面非常的广,不仅有大厂的面试真题,还有小白的入门算法题,对刚刚接触计算机语言的人来说非常的友好,所以博主决定开启一个专栏,详细记录在牛客网的刷题思路讲解,大家一起跟随博主走入算法的大门吧!
👉点击开始免费刷题,跟着博主走上巅峰💪


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


Java题目练习


⭕题目一: 计算一元二次方程


在这里插入图片描述


🌟代码演示

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            double a = scanner.nextDouble();
            double b = scanner.nextDouble();
            double c = scanner.nextDouble();
            double delta = Math.pow(b,2) - 4.0 * a * c;
            double x1 = (-b + Math.sqrt(delta)) / (2 * a);
            double x2 = (-b - Math.sqrt(delta)) / (2 * a);
            double realPart = (-b) / (2 * a);
            double imaginaryPart = Math.abs(Math.sqrt(-delta) / (2 * a)) ;
            if(a == 0.0){
                System.out.println("Not quadratic equation");
            }else{
               if(delta == 0.0){
                   if(x1 == 0.0){
                       System.out.println("x1=x2=" + String.format("%.2f",Math.abs(x1)));
                   }else{
                       System.out.println("x1=x2=" + String.format("%.2f",x1));
                   }
               }
                if(delta > 0.0){
                   System.out.println("x1=" + String.format("%.2f",Math.min(x1,x2)) + ";x2=" + String.format("%.2f",Math.max(x1,x2)));
               }
                if(delta < 0.0){
                    System.out.println("x1=" + String.format("%.2f",realPart) + "-" + String.format("%.2f",imaginaryPart) + "i;x2=" + String.format("%.2f",realPart) + "+" + String.format("%.2f",imaginaryPart) + "i");
                }
            }
            
      }
    }
}



💯思路解析

       本题目思路是:"按照题目来即可"

⭕题目二: 获得月份天数


在这里插入图片描述


🌟代码演示

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int y = sc.nextInt();
            int run = 0;

            if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
                System.out.println("31");
            } else {
                if (y == 2) {
                    if (n % 4 == 0 && n % 100 != 0) {
                        System.out.println("29");
                    } else if (n % 1000 == 0) {
                        System.out.println("29");
                    } else {
                        System.out.println("28");
                    }
                } else {
                    System.out.println("30");
                }
            }
        }
    }
}



💯思路解析

          本题目思路是:"直接按照题目来即可"

⭕题目三: 小乐乐是否被叫家长


在这里插入图片描述


🌟代码演示

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt(),y=sc.nextInt(),z=sc.nextInt();
        System.out.println((x+y+z)/3<60?"YES":"NO");
    }
}  

💯思路解析

       本题目思路是:"按照题目来即可"

⭕题目四: [NOIP2008]ISBN号码


在这里插入图片描述


🌟代码演示

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        int count=0,idx=1;
        for(int i=0;i<str.length()-2;i++){
            String s=str.substring(i,i+1);
            if(!s.equals("-")){
                int x=Integer.parseInt(s);
                count+=x*idx;
                idx++;
            }
        }
        int mod=count%11;
        String modStr="";
        if(mod==10) modStr="X";
        else modStr=String.valueOf(mod);
        String strCode=str.substring(str.length()-1);
//         System.out.println(strCode);
        
        if(strCode.equals("X")){
            if(mod==10){
                System.out.println("Right");
            }else{
                System.out.println(str.substring(0,str.length()-1)+modStr);                                                                    
            }
        }else{
            if(modStr.equals(strCode)){
                System.out.println("Right");
            }else{
                System.out.println(str.substring(0,str.length()-1)+modStr);
            }
        }
    }
}



💯思路解析

       本题目思路是:"直接按照题目来即可"

⭕题目五: 简单计算器


在这里插入图片描述


🌟代码演示

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String exp = scanner.nextLine();
        String op = "+-*/";
        int i;
        char ch = '0';
        for (i = 0; i < exp.length(); i++) {
            ch = exp.charAt(i);
            if (ch != '.' && (ch < '0' || ch > '9')) {
                break;
            }
        }
        if (op.contains(ch + "")) {
            double num1 = Double.parseDouble(exp.substring(0, i));
            double num2 = Double.parseDouble(exp.substring(i + 1));
            double result;
            if (ch == '/' && num2 == 0.0) {
                System.out.println("Wrong!Division by zero!");
            } else {
                switch (ch) {
                    case '+':
                        result = num1 + num2;
                        break;
                    case '-':
                        result = num1 - num2;
                        break;
                    case '*':
                        result = num1 * num2;
                        break;
                    case '/':
                        result = num1 / num2;
                        break;
                    default:
                        result=0;
                }
                System.out.printf("%.4f%c%.4f=%.4f", num1, ch, num2,result);
            }
        } else {
            System.out.println("Invalid operation!");
        }

    }
}





💯思路解析

       本题目思路是:"按照题目来就行"

✍ 结语

多刷刷题目,才能早日迈入大厂,巩固我们学习到的知识,下一期见,订阅专栏刷题不迷路
👉点击开始免费刷题,跟着博主走上巅峰💪


作者:KJ.JK

文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到