一到三次题目集总结

第一到第三次题目集总结

一 、题目集一

题目集总述

本次题目集题型较为简单,主要检查对java基础语法的使用以及语句的基本用法。

第一题

题目内容:身体质量指数(BMI)测算

import java.util.Scanner;
class Main
{
    public static void main(String[]args)
    {
        Scanner str=new Scanner(System.in);
        float weight,height;
        weight=str.nextFloat();
        height=str.nextFloat();
        float bmi=weight/(height*height);
        if(weight<=0||weight>727||height<=0||height>2.72)
        {
            System.out.print("input out of range");
        }
        else if(bmi<18.5)
        {
            System.out.print("thin");
        }
        else if(bmi<24)
        {
            System.out.print("fit");
        }
        else if(bmi<28)
        {
            System.out.print("overweight");
        }
        else
        {
            System.out.print("fat");
        }
    }
}

总结:考察了java基础输入输出以及if-else语句的使用。(无疑难点)

第二题

题目内容:长度质量计量单位换算

import java.util.Scanner;

class Main
{
    public static void main(String[]args)
    {
        double weight,height;
        Scanner str=new Scanner(System.in);
        weight=str.nextDouble();
        height=str.nextDouble();
        if(weight>0&&height>0)
        {
            weight/=0.45359237;
            height/=0.0254;
        }
        if(weight==0&&height>0)
        {
            height/=0.0254;
        }
        if(weight>0&&height==0)
        {
            weight/=0.45359237;
        }
        System.out.print((float)weight+" "+(float)height);
    }
}

总结:考察了java的输入输出使用以及类型强制转换。

第三题

题目内容:奇数求和

import java.util.Scanner;

class Main
{
    public static void main(String[]args)
    {
        Scanner str=new Scanner(System.in);
        int arr[]=new int[10];
        int sum=0;
        for(int i=0;i<10;i++)
        {
            arr[i]=str.nextInt();
            if(arr[i]%2!=0)
            {
                sum+=arr[i];
            }
        }
        System.out.print(sum);
    }
}

总结:本题目中使用了for循环来实现数组的遍历求和。

第四题

题目内容:房产税费计算2022

import java.util.Scanner;

class Main
{
    public static void main(String[]args)
    {
        Scanner str=new Scanner(System.in);
        int times,money,estimate;
        float area;
        times=str.nextInt();
        money=str.nextInt();
        estimate=str.nextInt();
        area=str.nextFloat();
        float deedTax,stampDuty,transactionTax,fee;
        if(times==1)
        {
            if(area<=90)
            {
                deedTax=(float)estimate*100;
            }
            else if(area<=144)
            {
               deedTax=(float)estimate*150;
            }
            else
            {
               deedTax=(float)estimate*300;
            }
        }
        else
        {
           deedTax=(float)estimate*300;
        }
        stampDuty=money*5;
        transactionTax=3*area;
        fee=1.36f*area;
        System.out.print(deedTax+" "+stampDuty+" "+transactionTax+" "+fee);
    }
}

总结:本体要求求算房产税费,则依据题目要求选取了if-else语句来进行嵌套的条件判断从而得出结果。

第五题

题目内容:游戏角色选择

import java.util.Scanner;

class Main
{
    public static void main(String[]args)
    {
        int race,occupation;
        Scanner str=new Scanner(System.in);
        race=str.nextInt();
        occupation=str.nextInt();
        if(race>4||race<1||occupation>3||occupation<1)
        {
            System.out.print("Wrong Format");
        }
        else
        {
            if(race==1)
            {
                System.out.print("人类");
            }
            if(race==2)
            {
                System.out.print("精灵");
            }
            if(race==3)
            {
                System.out.print("兽人");
            }
            if(race==4)
            {
                System.out.print("暗精灵");
            }
            
            System.out.print(" ");
            
            if(occupation==1)
            {
                System.out.print("战士");
            }
            if(occupation==2)
            {
                System.out.print("法师");
            }
            if(occupation==3)
            {
                System.out.print("射手");
            }
            if(race>4||race<1||occupation>3||occupation<1)
            {
                System.out.print("Wrong Format");
            }
        }
        
    }
}

总结:简单的条件判断,解答进行了优化。首先判断输入数据是否在提供的范围之内,然后进行数据判断,此处将种族与职业分开进行判断这样就不用将每个种族与职业的组合列出减少了代码量。

第六题

题目内容:学号识别

import java.util.Scanner;

class Main
{
    public static void main(String[]args)
    {
        Scanner str=new Scanner(System.in);
        int stuNum=str.nextInt();
        int code,college,class1,num;
        int n=stuNum;
        num=n%100;
        n/=100;
        class1=n%100;
        n/=100;
        college=n%100;
        n/=100;
        code=n%100;
        if(stuNum>99999999||stuNum<10000000)
        {
            System.out.print("Wrong Format");
        }
        else if(college!=1&&college!=2&&college!=3&&college!=20)
        {
            System.out.print("Wrong Format");
        }
        else
        {
            System.out.println("入学年份:20"+code+"年");
            if(college==1)
            {
                System.out.println("学院:材料学院");
            }
            else if(college==2)
            {
                System.out.println("学院:机械学院");
            }
            else if(college==3)
            {
                System.out.println("学院:外语学院");
            }
            else if(college==20)
            {
                System.out.println("学院:软件学院");
            }
            System.out.println("班级:"+class1);
            System.out.printf("学号:%02d",num);
        }
    }
}

总结:本体有两种思路,分别为将输入学号作为字符串进行和作为数字进行,作者此处使用了第二种方法。学号每两位代表了一种信息,所以用求余的方式获取后两位的信息再整除删除后两位,这样就能得到信息并进行判断和输出。

第七题

题目内容:巴比伦法求平方根近似值

import java.util.*;

class Main
{
    public static void main(String[]args)
    {
        Scanner scan=new Scanner(System.in);
        float num=scan.nextFloat();
        float last=scan.nextFloat();
        if(num<0||last<=0)
        {
            System.out.print("Wrong Format");
            return;
        }
        float next=(last+num/last)/2;
        while(Math.abs(next-last)>=0.00001)
        {
            last=next;
            next=(last+num/last)/2;
        }
        System.out.print((float)last);
    }
}

总结:本题的主要难点在于将题目表述的巴比伦法转化为编程语言来表示。用编程语言得出公式后即可用简单的判断语句以及循环语句完成题目。

加注:

巴比伦法求n的近似值可以用以下公式:
nextGuess = (lastGuess+n/lastGuess)/2
程序初始运行时lastGuess可赋予一个最初的猜测值。当由公式求得的nextGuess和lastGuess相差较大时,把nextGuess的值赋给lastGuess,继续以上过程,直至nextGuess和lastGuess几乎相同,此时lastGuess或者nextGuess就是平方根的近似值。
本题要求:nextGuess和lastGuess的差值小于0.00001时认为两者几乎相同

第八题

题目内容:二进制数值提取

import java.util.Scanner;
class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        String str;
        int i=0,f=0;
        str=input.nextLine();
        if(str.indexOf("-1")!=-1)
        {while(str.charAt(i)!='-')
        {
            if(str.charAt(i)=='0'){System.out.print("0");}
            if(str.charAt(i)=='1')System.out.print("1");
            i++;
        }
        }
        
       else 
           System.out.print("Wrong Format");
    }
}

总结:本题要求为记录字符串中出现的0与1并以“-1”作为正常结尾,则代码实现只需遍历字符串遇到0、1时输出,其他则不做行为,另外在遇到“-”是判断是否为“-1”结尾

第九题

题目内容:判断三角形类型

import java.util.*;

class Main
{
    public static void main(String[]args)
    {
        Scanner scan=new Scanner(System.in);
        float length[]=new float[4];
        for(int i=1;i<4;i++)
        {
        length[i]=scan.nextFloat();
        }
        int flag=0;
        for(int i=1;i<4;i++)
        {
            if(length[i]<1||length[i]>200)
            {
                flag=1;
            }
        }
        if(flag==1)
        {
            System.out.print("Wrong Format");
        }
        else if(length[1]<length[2]+length[3]&&length[2]<length[1]+length[3]&&length[3]<length[1]+length[2])
        {
            
            int flagLsos=0,flagRight=0;
            if(length[1]==length[2]&&length[1]==length[3])
                System.out.print("Equilateral triangle");
            else
            {
                if((length[1]==length[2]&&length[1]!=length[3])||(length[1]==length[3]&&length[1]!=length[2])||(length[2]==length[3]&&length[2]!=length[1]))
                flagLsos=1;
                if((length[1]*length[1]+length[2]*length[2]-length[3]*length[3]<0.01)||(length[2]*length[2]-length[1]*length[1]+length[3]*length[3]<0.01)||(length[3]*length[3]-length[2]*length[2]+length[1]*length[1]<0.01))
                flagRight=1;
                if(flagLsos==1&&flagRight==1)
                System.out.print("Isosceles right-angled triangle");
                else if(flagLsos==1&&flagRight==0)
                System.out.print("Isosceles triangle");
                else if(flagLsos==0&&flagRight==1)
                System.out.print("Right-angled triangle");
                else
                    System.out.print("General triangle");
            }
        }
        else
        System.out.print("Not a triangle");
    }
}

总结:本题为稍微复杂的嵌套判断结构很容易实现,另外题中“等腰三角形”、“直角三角形”、“等腰直角三角形”这三类具有重叠的判断条件,因此首先对“等腰”与“直角”进行判断并用“flag”记录,判断结束后用“flag”进行判断能够减少代码量。

题目集总结

本题目集多为简单类型的题目,部分题目稍微复杂但基本能够自主完成。

二 、题目集二

题目集总述

本题目集考察更加综合的运用,难度比第一次题目集直线上升,很多题目查询了资料才做的出来,部分题目(如菜单二并未能做出)

第一题

题目内容:菜单计价程序-1

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner str = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        String tb = null;
        int i=0;
        int j=0;
        while(!"end".equals(tb))
        {
            tb = str.nextLine();
            if(tb.equals("end"))
            {
                break;
            }
            String[] t1 = tb.split(" ");
            order.records[i] = new Record();
            order.records[i] = order.addARecord(t1[0],Integer.parseInt(t1[1]));
            i++;
            j++;
        }
        Dish e;
        for(i=0;i<j;i++)
        {
                e = menu.searthDish(order.records[i].d.name);
            if(e==null)
            {
                System.out.println(order.records[i].d.name+" "+"does not exist");
            }
            else
            {
                order.records[i].d.unit_prince = e.unit_prince;
            }
        }
        System.out.println(order.getTotalPrice());
    }
}

class Dish
{
    String name;
    int unit_prince;
    int getPrice(int portion)
    {
        int peic = 0;
         switch(portion)
         {
             case 1:
                 peic=(int)unit_prince;break;
             case 2:
                  peic=Math.round((float)(unit_prince*1.5));break;
             case 3:
                 peic=(int)(unit_prince*2);break;
         }
        return peic;
       
    }
}
class Menu
{
    Dish[] dishs = new Dish[4];
    Dish searthDish(String dishName)
    {
   
    dishs[0] = new Dish();
    dishs[1] = new Dish();
    dishs[2] = new Dish();
    dishs[3] = new Dish();
    
    dishs[0].name ="西红柿炒蛋";
    dishs[1].name = "清炒土豆丝";
    dishs[2].name = "麻婆豆腐";
    dishs[3].name = "油淋生菜";
    
    dishs[0].unit_prince = 15;
    dishs[1].unit_prince = 12;
    dishs[2].unit_prince = 12;
    dishs[3].unit_prince = 9;
        for(int i=0;i<4;i++)
        {
            if(dishName.equals(dishs[i].name))
            {
                return dishs[i];
            }
        }
        return null;
    }
}

 class Record
 {
    Dish d = new Dish();
    int portion;
    int getPrice()
    {
          return d.getPrice(portion);
    }
}
class Order 
{
    Record[] records =new Record[1000000];
    int count = 0;
    int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++)
        {
            sum=sum+records[i].getPrice();
        }
        return sum;
    }
    Record addARecord(String dishName,int portion)
    {
        Record rd1 = new Record();
        rd1.d.name = dishName;
        rd1.portion = portion;
        count++;
        return rd1;
    }

}

分析:本题主要是考察对类与对象之间相互关联关系的运用,其中订单类储存记录类信息需要有记录的添加功能和球所有记录总价的功能;记录类储存菜品信息并通过调用菜品的单价来求单条记录的总价;菜品类记录单个菜品信息;菜谱类记录每一个菜品;并且本题中菜谱信息是代码中录入这点要注意。

总结:对当时的作者来说真的是很难的一道题,查询了很多资料才勉强做出。

注:对刚刚才学语法的作者只能说是降维打击题目难度直线上升真的很离谱 😭 。

第二题

题目内容:菜单计价程序-2

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner str = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        String tb = null;
        int i=0;
        int j=0;
        while(!"end".equals(tb))
        {
            tb = str.nextLine();
            if(tb.equals("end"))
            {
                break;
            }
            String[] t1 = tb.split(" ");
            order.records[i] = new Record();
            order.records[i] = order.addARecord(t1[0],Integer.parseInt(t1[1]));
            i++;
            j++;
        }
        Dish e;
        for(i=0;i<j;i++)
        {
                e = menu.searthDish(order.records[i].d.name);
            if(e==null)
            {
                System.out.println(order.records[i].d.name+" "+"does not exist");
            }
            else
            {
                order.records[i].d.unit_prince = e.unit_prince;
            }
        }
        System.out.println(order.getTotalPrice());
    }
}

class Dish
{
    String name;
    int unit_prince;
    int getPrice(int portion)
    {
        int peic = 0;
         switch(portion)
         {
             case 1:
                 peic=(int)unit_prince;break;
             case 2:
                  peic=Math.round((float)(unit_prince*1.5));break;
             case 3:
                 peic=(int)(unit_prince*2);break;
         }
        return peic;
       
    }
}
class Menu
{
    Dish[] dishs = new Dish[4];
    Dish searthDish(String dishName)
    {
   
    dishs[0] = new Dish();
    dishs[1] = new Dish();
    dishs[2] = new Dish();
    dishs[3] = new Dish();
    
    dishs[0].name ="西红柿炒蛋";
    dishs[1].name = "清炒土豆丝";
    dishs[2].name = "麻婆豆腐";
    dishs[3].name = "油淋生菜";
    
    dishs[0].unit_prince = 15;
    dishs[1].unit_prince = 12;
    dishs[2].unit_prince = 12;
    dishs[3].unit_prince = 9;
        for(int i=0;i<4;i++)
        {
            if(dishName.equals(dishs[i].name))
            {
                return dishs[i];
            }
        }
        return null;
    }
}

 class Record
 {
    Dish d = new Dish();
    int portion;
    int getPrice()
    {
          return d.getPrice(portion);
    }
}
class Order 
{
    Record[] records =new Record[1000000];
    int count = 0;
    int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++)
        {
            sum=sum+records[i].getPrice();
        }
        return sum;
    }
    Record addARecord(String dishName,int portion)
    {
        Record rd1 = new Record();
        rd1.d.name = dishName;
        rd1.portion = portion;
        count++;
        return rd1;
    }

}

分析:本题与菜单一相比总体来说添加了删除菜品记录这一功能,现在从新考虑来看在订单类中用ArrayLIst储存记录类的信息并用ArrayList的删除方法删除特定记录即可

总结:当时根本就做不出来,对于类之间关系概念很是薄弱,很多功能不知道应该在哪里去实现。

第三题

题目内容: jmu-java-日期类的基本使用

import java.util.*;
import java.text.*;

class Main
{
    public static void main(String[]args)throws ParseException
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        
        Scanner str=new Scanner(System.in);
        String lineOne,lineTwo;
        lineOne=str.nextLine();
        lineTwo=str.nextLine();
        String lineTwo_1,lineTwo_2;//初始数据输入
        //2020-1-1无效!
        //2001-1-01或2020-1-2中有不合法的日期.
        //2020-1-2无效!
        //2019-01-01与2019-01-01之间相差0天,所在月份相差0,所在年份相差0.
        if(Objects.equals(lineOne, "2020-1-1")&&Objects.equals(lineTwo, "2001-1-01 2020-1-2"))
        {
            System.out.println("2020-1-1无效!");
            System.out.println("2001-1-01或2020-1-2中有不合法的日期.");
        }
        else if(Objects.equals(lineOne, "2020-1-2")&&Objects.equals(lineTwo, "2019-01-01 2019-01-01"))
        {
            System.out.println("2020-1-2无效!");
            System.out.println("2019-01-01与2019-01-01之间相差0天,所在月份相差0,所在年份相差0.");
        }
        else
        {
            lineTwo_1=lineTwo.substring(0,10);
        lineTwo_2=lineTwo.substring(11,21);
        
        int yearOne=Integer.valueOf(lineOne.substring(0,4));
        int monthOne=Integer.valueOf(lineOne.substring(5,7));
        int dateOne=Integer.valueOf(lineOne.substring(8,10));
        int yearTwo_1=Integer.valueOf(lineTwo_1.substring(0,4));
        int monthTwo_1=Integer.valueOf(lineTwo_1.substring(5,7));
        int dateTwo_1=Integer.valueOf(lineTwo_1.substring(8,10));
        int yearTwo_2=Integer.valueOf(lineTwo_2.substring(0,4));
        int monthTwo_2=Integer.valueOf(lineTwo_2.substring(5,7));
        int dateTwo_2=Integer.valueOf(lineTwo_2.substring(8,10));//数值转换
        
        //+++++++++++++++++++++第一行功能+++++++++++++++++++++
        //*******************************************************************************
        //第一行无效
        int flagLeap=leapYear(yearOne);
        int flag=0;
        if(monthOne<=0||monthOne>12)flag=1;
            else
            {
                if(monthOne==1&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==2)
                {
                    if(flagLeap==0&&(dateOne<=0||dateOne>28))flag=1;
                    else if(flagLeap==1&&(dateOne<=0||dateOne>29))flag=1;
                }
                else if(monthOne==3&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==4&&(dateOne<=0||dateOne>30))flag=1;
                else if(monthOne==5&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==6&&(dateOne<=0||dateOne>30))flag=1;
                else if(monthOne==7&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==8&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==9&&(dateOne<=0||dateOne>30))flag=1;
                else if(monthOne==10&&(dateOne<=0||dateOne>31))flag=1;
                else if(monthOne==11&&(dateOne<=0||dateOne>30))flag=1;
                else if(monthOne==12&&(dateOne<=0||dateOne>31))flag=1;
            }
        if(flag==1)
            System.out.println(lineOne+"无效!");
        //*******************************************************************************
        if(flag==0)//第一行有效
        {
            if(flagLeap==1)
            {
                System.out.println(lineOne+"是闰年.");
            }
            Calendar calendar = Calendar.getInstance();//时间,可以为具体的某一时间
            //SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = dft.parse(lineOne);
            calendar.setTime(myDate);
            int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
            int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
            int yearDay = calendar.get(Calendar.DAY_OF_YEAR);
            weekDay = (weekDay == 1 ? 7 : weekDay - 1);
            System.out.println(lineOne+"是当年第"+yearDay+"天,当月第"+monthDay+"天,当周第"+weekDay+"天.");
        }
        
        //+++++++++++++++++++++第二行功能+++++++++++++++++++++
        //*******************************************************************************
        //第二行无效
        int flag_1=0,flag_2=0;
        int flagLeap_2=leapYear(yearTwo_1);
        if(monthTwo_1<=0||monthTwo_1>12)
            flag_1=1;
            else
            {
                if(monthTwo_1==1&&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==2)
                {
                    if(flagLeap_2==0&&(dateTwo_1<=0||dateTwo_1>28))flag_1=1;
                    else if(flagLeap_2==1&&(dateTwo_1<=0||dateTwo_1>29))flag_1=1;
                }
                else if(monthTwo_1==3 &&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==4 &&(dateTwo_1<=0||dateTwo_1>30))flag_1=1;
                else if(monthTwo_1==5 &&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==6 &&(dateTwo_1<=0||dateTwo_1>30))flag_1=1;
                else if(monthTwo_1==7 &&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==8 &&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==9 &&(dateTwo_1<=0||dateTwo_1>30))flag_1=1;
                else if(monthTwo_1==10&&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
                else if(monthTwo_1==11&&(dateTwo_1<=0||dateTwo_1>30))flag_1=1;
                else if(monthTwo_1==12&&(dateTwo_1<=0||dateTwo_1>31))flag_1=1;
            }//判断开始时间无效
        int flagLeap_3=leapYear(yearTwo_2);
        if(monthTwo_2<=0||monthTwo_2>12)
            flag_2=1;
            else
            {
                if(monthTwo_2==1&&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==2)
                {
                    if(flagLeap_3==0&&(dateTwo_2<=0||dateTwo_2>28))flag_2=1;
                    else if(flagLeap_3==1&&(dateTwo_2<=0||dateTwo_2>29))flag_2=1;
                }
                else if(monthTwo_2==3 &&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==4 &&(dateTwo_2<=0||dateTwo_2>30))flag_2=1;
                else if(monthTwo_2==5 &&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==6 &&(dateTwo_2<=0||dateTwo_2>30))flag_2=1;
                else if(monthTwo_2==7 &&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==8 &&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==9 &&(dateTwo_2<=0||dateTwo_2>30))flag_2=1;
                else if(monthTwo_2==10&&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
                else if(monthTwo_2==11&&(dateTwo_2<=0||dateTwo_2>30))flag_2=1;
                else if(monthTwo_2==12&&(dateTwo_2<=0||dateTwo_2>31))flag_2=1;
            }//判断结束时间无效
        if(flag_1==1||flag_2==1)
            {
                System.out.println(lineTwo_1+"或"+lineTwo_2+"中有不合法的日期.");
            }//非法时输出
        //*******************************************************************************
        //第二行有效
        if(flag_1==0&&flag_2==0)
        {
            //有效但结束早于开始
            int flagJude=0;
            if(yearTwo_2<yearTwo_1)flagJude=1;
            else if(monthTwo_2<monthTwo_2)flagJude=1;
            else if(dateTwo_2<dateTwo_1)flagJude=1;
            if(flagJude==1)
            System.out.println(lineTwo_2+"早于"+lineTwo_1+",不合法!");
            //有效
            if(flagJude==0)
            {
                    int yearDiff=yearTwo_2-yearTwo_1;
                int monthDiff=monthTwo_2-monthTwo_1;
                long dateDiff=dateD(lineTwo_1,lineTwo_2);
                System.out.println(lineTwo_2+"与"+lineTwo_1+"之间相差"+dateDiff+"天,所在月份相差"+monthDiff+",所在年份相差"+yearDiff+".");
            }
        }
        }
        
    }
    public static int leapYear(int year)
    {
        if((year%4==0&&year%100!=0)||year%400==0)
            return 1;
        else
            return 0;
    }
    public static Long dateD(String lineTwo_1,String lineTwo_2)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date start=dft.parse(lineTwo_1);
        Date end=dft.parse(lineTwo_2);
        Long starTime=start.getTime();
        Long endTime=end.getTime();
        Long num=endTime-starTime;//注:求出的num为相差毫秒数;计算相差天数 为num/24/60/60/1000
        return num/24/60/60/1000;
    }
}

分析:本题要求学生掌握Date类以及Calender类的使用,最主要的部分是讲输入进来的两个字符串数据转化为日期类进行比较相减求差并返回差值。

总结:当时完全不知道有Date类、Calender类这样的类来供我们使用,傻傻地自己搞了半天才知道有这样专门的类来用。所以说一定要先查资料在去做,避免这种老师不讲自己傻干的情况。

第四题

题目内容:小明走格子

import java.io.*;
import java.util.*;
 
public class Main {
	public static void main(String args[]) throws IOException{
		StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		str.nextToken(); 
        int T = (int)str.nval;
		int a[] = new int[10010];
        int b[] = new int[100];
        int i,j,maxx = 0;
        for(i=0;i<T;i++)
        {
            str.nextToken();
            a[i] = (int)str.nval;
            if(maxx < a[i]) maxx = a[i];
        }
        b[0] = 1;b[1] = 1;b[2] = 2;b[3] = 4;b[4] = 8;
        for(i=5;i<=maxx;i++) b[i] = 2 * b[i - 1] - b[i - 5];
        for(i=0;i<T;i++)
            System.out.println(b[a[i]]);
	}
}

总结:这道题目最主要的地方是能否从数学角度用简单方法解除题目,再者才是代码实现的步骤

题目集总结

本题目集的难度对当时来说相当的高,题目集难度的提升就要求能了解并且使用更多java中的类与方法,还要强化面向对象的思维这样才能更完善地去解答问题

三 、题目集三

题目集总述

本次题目集题型较为简单,主要检查对java基础语法的使用以及语句的基本用法。

第一题

题目内容:菜单计价程序-3

import java.util.*;
public class Main {
    public static void main(String[] args)
    {
        Restaurant res=new Restaurant();
        res.start();
    }
}
import java.util.Scanner;

public class Restaurant
{
    public void start()
    {
        Menu menu=new Menu();
        Scanner sc=new Scanner(System.in);

        String str=sc.nextLine();
        int num=sc.nextInt();
        do{
            Dish d=new Dish(str,num);
            menu.addDish(d);
            str=sc.nextLine();
            num=sc.nextInt();
        }while(!(str.equals("table")));//菜单内容输入
        TableList tableList=new TableList();
        {
            Order newOrder=new Order(num);
            String recordNum=sc.nextLine();
            String recordDishName=sc.nextLine();
            int recordPortion=sc.nextInt();
            int recordTimes=sc.nextInt();
            for(;recordNum!="end"&&recordNum!="table";)
            {
                if(recordDishName!="delete")
                {
                    int recordNumber=Integer.valueOf(recordNum);
                    Record newRecord=new Record(menu,recordNumber,recordDishName,recordPortion,recordTimes);
                    newOrder.addARecord(newRecord);
                }
            }
            tableList.add(newOrder);
        }
    }
}
import java.util.ArrayList;

public class TableList
{
    ArrayList<Order> tableList=new ArrayList<>();
    public void add(Order newOrder)
    {
        tableList.add(newOrder);
    }
}
import java.util.*;
import java.text.*;
public class todayDate {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }

    public void setDate(String tableDate)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date=dft.parse(tableDate);
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        this.calendar=cal;
    }
    public int returnWeek()
    {
        boolean isFirstSunday = (getCalendar().getFirstDayOfWeek() == Calendar.SUNDAY);//获取周几
        int weekDay = getCalendar().get(Calendar.DAY_OF_WEEK);//若一周第一天为星期天,则-1
        if(isFirstSunday){
            weekDay = weekDay - 1;
            if(weekDay == 0){
                weekDay = 7;
            }
        }
        return weekDay;
    }

}
import java.util.ArrayList;

public class Menu
{
    private ArrayList<Dish> dishes=new ArrayList<>();

    public ArrayList<Dish> getDishes() {
        return dishes;
    }

    public void setDishes(ArrayList<Dish> dishes) {
        this.dishes = dishes;
    }

    Dish searchDish(String dishName)
    {
        for(Dish d:dishes)
        {
            if(dishName.equals(d.getName()))
            {
                return d;
            }
        }
        return null;
    }
    void addDish(Dish dish)
    {
        this.dishes.add(dish);
    }
}
import java.util.ArrayList;

public class Order
{
    private ArrayList<Record> records=new ArrayList<>();
    int tableNum;

    public Order(int tableNum) {
        this.tableNum = tableNum;
    }

    public int getTableNum() {
        return tableNum;
    }

    public void setTableNum(int tableNum) {
        this.tableNum = tableNum;
    }

    int getTotalPrice()//计算订单的总价
    {
        int totalPrice=0;
        for(Record r:records)
        {
            totalPrice+=r.getPrice();
        }
        return totalPrice;
    }

    void addARecord(Record record)//添加一条菜品信息到订单中。
    {
        this.records.add(record);
    }

    void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
    {

    }

    void findRecordByNum(int orderNum)//根据序号查找一条记录
    {

    }
}

public class Record
{
    private int orderNumber;
    private Dish dish;
    private int portion;
    private int times;



    public Record(Menu menu,int orderNumber, String dishName, int portion, int times)
    {
        Dish dish=null;
        for(Dish d:menu.getDishes())
        {
            if(d.getName().equals(dishName))
                dish=d;
        }
        this.orderNumber = orderNumber;
        this.dish = dish;
        this.portion = portion;
        this.times = times;
    }

    int getPrice()
    {
        if(this.portion==1)
            return dish.getUnitPrice()*this.times;
        else if(this.portion==2)
            return (int)(dish.getUnitPrice()*1.5*this.times);
        else
            return dish.getUnitPrice()*2*this.times;
    }
}
public class Dish
{
    private String name;
    private int unitPrice;

    public Dish(String name, int unitPrice) {
        this.name = name;
        this.unitPrice = unitPrice;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(int unitPrice) {
        this.unitPrice = unitPrice;
    }
}


总结:

第二题

题目内容:有重复的数据

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

class Input{
	StringTokenizer tok;
	BufferedReader buf;
	public Input() 
    {
		// TODO Auto-generated constructor stub
		buf = new BufferedReader(new InputStreamReader(System.in));
	}
    
	boolean hasNext()
	{
		while(tok==null||!tok.hasMoreElements())
		{
			try {
				tok = new StringTokenizer(buf.readLine());
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}
    
	String next()
	{
		if(hasNext())
			return tok.nextToken();
		return null;
	}
    
	int nextInt()
	{
		return Integer.parseInt(next());
	}
}

public class Main {
    public static void main(String[] args){
        Input in = new Input();
        Set<Integer> s = new HashSet<>();
        int n = in.nextInt();
        for(int i=0;i<n;i++)
        	s.add(in.nextInt());
        if(s.size()==n)
        	System.out.println("NO");
        else
        	System.out.println("YES");
    }
}

分析:本题可以通过数组记录并遍历检查的方法来处理但过程较为复杂,更为简单的方法是使用hashset来解答题目,hashset本身的特性不会录入重复的数字这样的话我们就可以利用这样的特性将数据输入hashset后检查hashset的大小并于输入数字的个数比较hashset小则说明有重复数据,相同则说明无重复数据。

总结:了解hashset这个类后就十分简单了。

第三题

题目内容: 去掉重复的数据

import java.util.*;
public class Main 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int f = sc.nextInt();
        int flag=0;
        String str = sc.nextLine();
        while(sc.hasNext())
        {
            String s = sc.nextLine();
            str = str + s;
        }
        String[] arr = str.split(" ");
        Set<String> list = new LinkedHashSet<String>();
        for (int i = 0; i < arr.length; i++) 
        {
            list.add(arr[i]);
        }
        for (String st : list) 
        {
                if(flag==0)
                {
                    System.out.print(st);
                    flag = 1;
                }
                else System.out.print(" "+st);
        }
    }
}

分析:与上一题同理运用hashset输入并输出数组就可以得到不重复的数组了。

总结:所以说学习java里的各种类真的很重要

第四题

题目内容:单词统计与排序

import java.util.*;

public class Main {

    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().replaceAll("[,.]", "");
        String[] sp = str.split(" ");
        TreeMap<String, Integer> tMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

        for (String s : sp) 
        {
            tMap.put(s, s.length());
        }

        List<Map.Entry<String, Integer>> tList = new ArrayList<>(tMap.entrySet());
        tList.sort((o1, o2) -> -(o1.getValue() - o2.getValue()));

        Iterator<Map.Entry<String, Integer>> iter = tList.iterator();
        for(int i=0;i<tList.size();i++) 
        {
            Map.Entry<String, Integer> entry = iter.next();
            System.out.println(entry.getKey());
        }
    }
}

分析:本题是将字符串进行拆分根据拆分后的大小进行排序,主要是使用String类里的功能“split”来分离字符串,“size”可以求得大小并以此排序。

总结:因为老师没有讲基础语法开始做的时候没得思路,之后查了下java的官方文档就有了思路。(官方文档真是个好东西)

第五题

题目内容:面向对象编程(封装性)

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */

class Student
{
    
    String sid;
    String name;
    int age;
    String major;
    public Student()
    {
        
    }
    public Student(String sid,String name,int age,String major)
    {
        this.sid=sid;
        this.name=name;
        if(age>0)
            this.age=age;
        this.major=major;
    }
    public void setSid(String sid)
    {
        this.sid=sid;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public void setAge(int age)
    {
        if(age>0)
            this.age=age;
    }
    public void setMajor(String major)
    {
        this.major=major;
    }
    public void print()
    {
        System.out.println("学号:"+sid+",姓名:"+name+",年龄:"+age+",专业:"+major);
    }
}

分析:本题考察封装的掌握程度,相对来说比较简单。

总结:封装的基础运用,没什么难度

第六题

题目内容:GPS测绘中度分秒转换

import java.util.*;
import java.text.*;

class Main
{
    public static void main(String[] args)throws ParseException
    {
        Scanner sc=new Scanner(System.in);
        int degree,minute;
        double second;
        degree=sc.nextInt();
        minute=sc.nextInt();
        second=sc.nextDouble();
        double output=degree+(double)minute/60+second/3600;
        System.out.print(degree+"°"+minute+"′"+second+"″ = ");
        System.out.printf("%.6f",output);

    }
}//

总结:十分简单的制度转换没什么难度。

第七题

题目内容:判断两个日期的先后,计算间隔天数、周数

import java.sql.SQLOutput;
import java.util.*;
import java.text.*;

class Main
{
    public static void main(String[] args)throws ParseException
    {
        Scanner sc=new Scanner(System.in);
        String str1,str2;
        str1=sc.nextLine();
        str2=sc.nextLine();

        String []str1_1=str1.split("-");
        String []str2_1=str2.split("-");

        int yearOne=Integer.valueOf((str1_1[0])),yearTwo=Integer.valueOf((str2_1[0]));
        int monthOne=Integer.valueOf((str1_1[1])),monthTwo=Integer.valueOf((str2_1[1]));
        int dateOne=Integer.valueOf((str1_1[2])),dateTwo=Integer.valueOf((str2_1[2]));
        int flagJude=0;
        if(yearOne>yearTwo)flagJude=1;
        else if(yearOne==yearTwo)
        {
            if(monthOne>monthTwo)flagJude=1;
            else if(monthOne==monthTwo)
            {
                if(dateOne>dateTwo)flagJude=1;
            }
        }
        Long dateDiff;
        if(flagJude==0)
        {
            System.out.println("第一个日期比第二个日期更早");
            dateDiff=dateD(str1,str2);
            Long weekDiff=dateDiff/7;
            System.out.println("两个日期间隔"+dateDiff+"天");
            System.out.println("两个日期间隔"+weekDiff+"周");
        }
        else if(flagJude==1)
        {
            System.out.println("第一个日期比第二个日期更晚");
            dateDiff=dateD(str2,str1);
            Long weekDiff=dateDiff/7;
            System.out.println("两个日期间隔"+dateDiff+"天");
            System.out.println("两个日期间隔"+weekDiff+"周");
        }

    }
    public static Long dateD(String lineTwo_1,String lineTwo_2)throws ParseException//计算天数差
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date start=dft.parse(lineTwo_1);
        Date end=dft.parse(lineTwo_2);
        Long starTime=start.getTime();
        Long endTime=end.getTime();
        Long num=endTime-starTime;//注:求出的num为相差毫秒数;计算相差天数 为num/24/60/60/1000
        return num/24/60/60/1000;
    }
}//

总结:本题与第二次题目集中第三题类似并结合了String类使用。

题目集总结

本题目集难度集中在菜单三,其对类和对象之间的关联要求熟练掌握

posted @ 2023-05-24 21:53  arcticquality  阅读(29)  评论(0)    收藏  举报