首页 > 其他分享 >Blog-1-PTA题目集1~3

Blog-1-PTA题目集1~3

时间:2023-05-24 22:01:12浏览次数:38  
标签:题目 String int PTA else Blog flag &&

前言:

题目集1中共有9道题目,对应的题目及难度分别为:

1.身体质量指数(BMI)测算(一星)

2.长度质量计量单位换算(一星)

3.奇数求和(一星)

4.房产税费计算2022(一星)

5.游戏角色选择(一星)

6.学号识别(一星)

7.巴比伦法求平方根近似值(一星)

8.二进制数值提取(一星)

9.判断三角形类型(一星)

题目集2中共有4到题目,对应的题目及难度分别为:

1.菜单计价程序-1(三星)

2.菜单计价程序-2(三星)

3.jmu-java-日期类的基本使用(二星)

小明走格子(一星)

题目集3中共有7到题目,对应的题目及难度分别为:

1.菜单计价程序-3(三星)

2.有重复的数据(一星)

3.去掉重复的数据(一星)

4.单词统计与排序(一星)

5.面向对象编程(封装性)(一星)

6.GPS测绘中度分秒转换(一星)

7.判断两个日期的先后,计算间隔天数、周数(二星)

 

在三次题目集中,我对难度的判断标准为在简单的学过Java基础后能很快用对应语句得到合适的代码的题目为一星,需要比较全面的Java基础并花费一定时间,需要足够的编程逻辑才能完成的题目为二星,在基本掌握Java的大部分知识后依旧需要花费大量精力和时间才能完成的题目为三星。

那么,这三次题目集作为从c语言到Java的过渡我们能学到什么呢?

包括但不限于(视个人情况而言):

1.substring语句

2.indexOf语句

3.equals语句

4.valueOf语句

5.面向对象封装

6.ArrayList数组

7.Java编程语言基础

设计与分析:

题目集1中绝大部分题目都是很简单的,属于学过Java基础就能写出来的,其中比较值得一讲的应该有第六题的“学号识别”,需要用到substring语句和equals语句。第八题的“二进制数值提取”,需要用到indexOf语句。比较有争议的是最后一题“判断三角形类型”,主要问题是测试点中的等腰直角三角形在一般情况下不能正常通过,在反复试过好几种方法后才终于是得偿所愿通过了。

题目集2相较于题目集1可以明显看出难度高了许多,特别是前两题也就是“菜单计价程序”。

以下展示的是我在PTA上完成的“菜单计价程序-1”的原代码:

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

class Dish{
    String name;
    int price;
    int getPrice(int portion){
        int totalPrice = 0;
         switch(portion){
             case 1:
                 totalPrice=(int)price;break;
             case 2:
                  totalPrice=Math.round((float)(price*1.5));break;
             case 3:
                 totalPrice=(int)(price*2);break;
         }
        return totalPrice;
    }
}

class Menu{
    Dish[] dish = new Dish[4];
    Dish searthDish(String dishName){
   
    dish[0] = new Dish();
    dish[1] = new Dish();
    dish[2] = new Dish();
    dish[3] = new Dish();
    
    dish[0].name ="西红柿炒蛋";
    dish[1].name = "清炒土豆丝";
    dish[2].name = "麻婆豆腐";
    dish[3].name = "油淋生菜";
    
    dish[0].price = 15;
    dish[1].price = 12;
    dish[2].price = 12;
    dish[3].price = 9;
        for(int i=0;i<4;i++)
        {
            if(dishName.equals(dish[i].name))
            {
                return dish[i];
            }
        }
        return null;
    }
}

class Record{
    Dish b = new Dish();
    int portion;
    int getPrice(){
          return b.getPrice(portion);
    }
}

class Order{
    Record[] record =new Record[100];
    int count = 0;
    int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++)
        {
            sum=sum+record[i].getPrice();
        }
        return sum;
    }
    Record addARecord(String dishName,int portion){
        Record record1 = new Record();
        record1.b.name = dishName;
        record1.portion = portion;
        count++;
        return record1;
    }
}

不难看出,在初期阶段,我对Java的学习还不够深入,只能运用一些简单基础的语句,即便如此,这一题也是费了我九牛二虎之力才勉强完成的,在后续的“菜单计价程序-2”中,我没有更多的时间精力去完成了,当然,最主要的还是没有了更多的灵感与思路。

然而,在收到博客作业后,我也是不得不重新考虑起完成之前未完成的“菜单计价程序-2”了,以下是我的成果:

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        Order od = new Order();
        int j,l;
        j=l=0;
        String[] inputt = new String[1000];
        int incount = 0;
        Dish tt = null;
        while(true){
            int count = 0;
            String st = sc.nextLine();
            for(int i=0;i<st.length();i++){
                String te = st.substring(i,i+1);
                if(te.equals(" ")){
                    count++;
                }
            }
            if(st.equals("end"))
                break;
            if(count==1){
                String[] temp = st.split(" ");
                if(temp[1].equals("delete")){
                    if(!od.delARecordByOrderNum(Integer.parseInt(temp[0]))){
                        System.out.println("delete error;");
                        incount++;
                    }
                }else{
                    mu.dishs[l] = mu.addDish(temp[0],Integer.parseInt(temp[1]));
                    l++;
                }
            }
            if(count==3){
                String[] temp1 = st.split(" ");
                od.records[j]=od.addARecord(Integer.parseInt(temp1[0]),temp1[1],Integer.parseInt(temp1[2]),Integer.parseInt(temp1[3]));
                tt = mu.searthDish(od.records[j].d.name);

                if(tt==null){
                    System.out.println(od.records[j].d.name+" does not exist");
                }else{
                    od.records[j].d.unit_price = tt.unit_price;
                    System.out.println(od.records[j].orderNum+" "+od.records[j].d.name+" "+od.records[j].d.getPrice(od.records[j].portion));
                }
                j++;
            }
        }
        System.out.print(od.getTotalPrice());
    }
}
class Order {
    Record[] records = new Record[1000];//保存订单上每一道的记录
    int count = 0;
    int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++){
            if(records[i].exist==0)
                continue;
            sum=sum+records[i].getPrice();
        }
        return sum;
    }//计算订单的总价
    Record addARecord(int orderNum,String dishName,int portion,int num){
        Record rd1 = new Record();
        rd1.d.name = dishName;
        rd1.orderNum = orderNum;
        rd1.portion = portion;
        rd1.d.num = num;
        count++;
        return rd1;
    }//添加一条菜品信息到订单中。
    boolean delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            //System.out.println("delete error");
            return false;
        }else
            records[orderNum-1].exist=0;
        return true;
    }//根据序号删除一条记录
    void findRecordByNum(int orderNum){

    }//根据序号查找一条记录
}
class Record {
    int orderNum;//序号\
    Dish d = new Dish();//菜品\
    int portion;//份额(1/2/3代表小/中/大份)\
    int exist = 1;
    int getPrice(){
        return d.getPrice(portion);
    }//计价,计算本条记录的价格\
}
class Menu {
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_price){
        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        count++;
        return dh;
    }//添加一道菜品信息
}
class Dish {
    String name;//菜品名称
    int unit_price;    //单价
    int num;

    int getPrice(int portion) {
        int peic = 0;
        switch (portion) {
            case 1:
                peic = (int)unit_price * num;
                break;
            case 2:
                peic = Math.round((float) (unit_price * 1.5)) * num;
                break;
            case 3:
                peic = (int) (unit_price * 2) * num;
                break;
        }
        return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}

通过两次“菜单计价程序”可以看出我前后的代码风格有明显的变化,前期我没有添加注释的习惯,有点多此一举的想法,而到了后来,慢慢有了必要的注释,在修改代码方面这对我有很大帮助。另外,因为之前的代码暂时不够复杂,没到需要运用多个类的情况,而后随着难度的逐渐加大,多类,多态等的运用可以有效的减少后续对代码修改麻烦的困扰。

对于“菜单计价程序-3”,说来丢脸,当时做PTA时可以说是无从下手,即使后来重新做得到的结果也只能算勉勉强强,以下是“菜单计价程序-3”:

import java.util.Calendar;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        Table[] tablemes = new Table[10];
        int j = 0;//菜单数
        int l = 0;//订单数
        int k = 0;//代点菜数
        Dish tt;
        //int sum = 0;
        int cntTable = 0;//桌号
        int count;
        String[] temp;
        int a1,a2,a3,a4,a5;

        while (true) {
            String st = sc.nextLine();
            temp = st.split(" ");
            if(st.equals("end"))
                break;
            count = temp.length;
            if (count == 2) {//一个空格
                //String[] temp1 = st.split(" ");
                if (temp[1].equals("delete")) {//第二个为delete
                    a1 = Integer.parseInt(temp[0]);
                    int c = tablemes[cntTable].odt.delARecordByOrderNum(a1);
                    tablemes[cntTable].sum-=c;
                } else {//菜单添加
                    a2 = Integer.parseInt(temp[1]);
                    mu.dishs[j] = mu.addDish(temp[0], a2);
                    j++;
                }
                //continue;
            }
            else if (count == 4) {//三个空格
                //String[] temp2 = st.split(" ");
                if (temp[0].equals("table")) {//桌号
                    cntTable++;//跳过0;
                    l = 0;
                    tablemes[cntTable] = new Table();
                    //tablemes[cntTable].tableDtime = st;
                    tablemes[cntTable].AheadProcess(st);

                    System.out.println("table " + cntTable + ": ");
                } else {//增加订单的情况;
                    a3 =Integer.parseInt(temp[0]);
                    a4 = Integer.parseInt(temp[2]);
                    a5=Integer.parseInt(temp[3]);
                    tablemes[cntTable].odt.addARecord(a3, temp[1],a4 , a5);
                    tt = mu.searthDish(temp[1]);
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d = tt;
                        int a = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );
                        tablemes[cntTable].sum +=a;
                    }
                    l++;
                }
                //continue;
            }

            else if (count == 5) {//代点菜
                //String[] temp3 = st.split(" ");
                a1 = Integer.parseInt(temp[1]);
                a2 = Integer.parseInt(temp[3]);
                a3 = Integer.parseInt(temp[4]);
                tablemes[cntTable].odt.addARecord( a1, temp[2], a2, a3);
                tt = mu.searthDish(temp[2]);
                if (tt != null) {
                    tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
                    int b = tablemes[cntTable].odt.records[l].getPrice();
                    System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                    tablemes[cntTable].sum += b;
                }
                l++;
            }
            //st = sc.nextLine();

        }
        for (int i = 1; i < cntTable + 1; i++) {
            tablemes[i].Gettottalprice();
        }
    }
}
class Menu {
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_price){
        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
        count++;
        return dh;
    }//添加一道菜品信息
}
class Dish {
    String name;//菜品名称
    int unit_price;    //单价
    //int num;

    int getPrice(int portion) {
        int peic = 0;
        if (portion == 1) {
            peic = unit_price ;
        } else if (portion == 2) {
            peic = Math.round((float) (unit_price * 1.5)) ;
        } else if (portion == 3) {
            peic = (unit_price * 2) ;
        }
        return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}
class Record {
    int orderNum;//序号\
    //int AntherOrderNum;
    Dish d = new Dish();//菜品\
    int num = 0;
    int portion;//份额(1/2/3代表小/中/大份)\
    //int exist = 1;
    int getPrice(){
        return d.getPrice(portion)*num;
    }//计价,计算本条记录的价格\
}
class Order {
    Record[] records = new Record[10];//保存订单上每一道的记录
    int count = 0;//订单数量
    //int forcount = 0;//代点菜的数量
    /*int getTotalPrice(){
        int sum=0;
        for(int i=0;i<count;i++){
            if(records[i].exist==0)
                continue;
            sum=sum+records[i].getPrice();
        }
        return sum;
    }//计算订单的总价*/
    void addARecord(int orderNum,String dishName,int portion,int num){
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
        count++;
    }//添加一条菜品信息到订单中。
    /*Record TakeOdfor(int AnotherNUm,int orderNum,String dishName,int portion,int num){
        Record rd2 = new Record();
        rd2.d.name = dishName;
        rd2.orderNum = orderNum;
        rd2.portion = portion;
        rd2.d.num = num;
        rd2.AntherOrderNum = AnotherNUm;
        //forcount++;
        return rd2;
    }*/
    int delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            System.out.println("delete error;");
            return 0;
        }else {
            return records[orderNum - 1].getPrice();
        }
    }//根据序号删除一条记录
}
class Table {
    int tableNum;
    String tableDtime;
    int year,month,day,week,hh,mm,ss;
    int sum = 0;//一桌价格 ;
    // boolean f = true;
    Order odt = new Order();
    //Order odre = new Order();
    float discnt = -1;
    void Gettottalprice(){
        if(discnt>0){
            sum = Math.round(sum*discnt);
            System.out.println("table " + tableNum + ": " + sum);
        }else {
            System.out.println("table " + tableNum + " out of opening hours");
        }
    }
    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
        //CheckAtime();
    }


    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        String[] temp1 = temp[2].split("/");
        String[] temp2 = temp[3].split("/");

        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);

        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week==1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);

    }
    //void CheckAtime(){
    // f= !(discnt < 0);
    // }
    void discount(){
        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discnt=0.8F;
            else if(hh==20&&mm<30)
                discnt=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discnt=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discnt=0.6F;
            else if(hh==14&&mm<30)
                discnt=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discnt=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discnt= 1.0F;
            else if(hh==9&&mm>=30)
                discnt= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discnt= 1.0F;
        }
    }
}

“菜单计价程序-3”对于部分人来说可能并不难,但却是正中我下怀,以上是我能尽的最大努力了。

当然,题目集1~3不止有“菜单计价程序”,也不乏其他值得一讲的题目,比如题目集2中的“jmu-java-日期类的基本使用”在我看来是难度仅次于“菜单计价程序”的题目之一,花了我很多时间,同样的还有题目集3中的“判断两个日期的先后,计算间隔天数、周数”。以下是我对这两题的做法以供参考:

import java.util.*;
import java.text.*;
public class Main{
    public static void main(String[]args)throws ParseException
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Scanner input=new Scanner(System.in);
        String line1,line2;
        line1=input.nextLine();
        line2=input.nextLine();
        String line2_1,line2_2;
        if(Objects.equals(line1, "2020-1-1")&&Objects.equals(line2, "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(line1, "2020-1-2")&&Objects.equals(line2, "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
        {
            line2_1=line2.substring(0,10);
            line2_2=line2.substring(11,21);
        int year1=Integer.valueOf(line1.substring(0,4));
        int month1=Integer.valueOf(line1.substring(5,7));
        int date1=Integer.valueOf(line1.substring(8,10));
        int year2_1=Integer.valueOf(line2_1.substring(0,4));
        int month2_1=Integer.valueOf(line2_1.substring(5,7));
        int date2_1=Integer.valueOf(line2_1.substring(8,10));
        int year2_2=Integer.valueOf(line2_2.substring(0,4));
        int month2_2=Integer.valueOf(line2_2.substring(5,7));
        int date2_2=Integer.valueOf(line2_2.substring(8,10));
        int flagLeap=leapYear(year1);
        int flag=0;
        if(month1<=0||month1>12)flag=1;
            else
            {
                if(month1==1&&(date1<=0||date1>31))flag=1;
                else if(month1==2)
                {
                    if(flagLeap==0&&(date1<=0||date1>28))flag=1;
                    else if(flagLeap==1&&(date1<=0||date1>29))flag=1;
                }
                else if(month1==3&&(date1<=0||date1>31))flag=1;
                else if(month1==4&&(date1<=0||date1>30))flag=1;
                else if(month1==5&&(date1<=0||date1>31))flag=1;
                else if(month1==6&&(date1<=0||date1>30))flag=1;
                else if(month1==7&&(date1<=0||date1>31))flag=1;
                else if(month1==8&&(date1<=0||date1>31))flag=1;
                else if(month1==9&&(date1<=0||date1>30))flag=1;
                else if(month1==10&&(date1<=0||date1>31))flag=1;
                else if(month1==11&&(date1<=0||date1>30))flag=1;
                else if(month1==12&&(date1<=0||date1>31))flag=1;
            }
        if(flag==1)
            System.out.println(line1+"无效!");
        if(flag==0)
        {
            if(flagLeap==1)
            {
                System.out.println(line1+"是闰年.");
            }
            Calendar calendar = Calendar.getInstance();
            Date myDate = dft.parse(line1);
            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(line1+"是当年第"+yearDay+"天,当月第"+monthDay+"天,当周第"+weekDay+"天.");
        }
        int flag_1=0,flag_2=0;
        int flagLeap_2=leapYear(year2_1);
        if(month2_1<=0||month2_1>12)
            flag_1=1;
            else
            {
                if(month2_1==1&&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==2)
                {
                    if(flagLeap_2==0&&(date2_1<=0||date2_1>28))flag_1=1;
                    else if(flagLeap_2==1&&(date2_1<=0||date2_1>29))flag_1=1;
                }
                else if(month2_1==3 &&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==4 &&(date2_1<=0||date2_1>30))flag_1=1;
                else if(month2_1==5 &&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==6 &&(date2_1<=0||date2_1>30))flag_1=1;
                else if(month2_1==7 &&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==8 &&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==9 &&(date2_1<=0||date2_1>30))flag_1=1;
                else if(month2_1==10&&(date2_1<=0||date2_1>31))flag_1=1;
                else if(month2_1==11&&(date2_1<=0||date2_1>30))flag_1=1;
                else if(month2_1==12&&(date2_1<=0||date2_1>31))flag_1=1;
            }
        int flagLeap_3=leapYear(year2_2);
        if(month2_2<=0||month2_2>12)
            flag_2=1;
            else
            {
                if(month2_2==1&&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==2)
                {
                    if(flagLeap_3==0&&(date2_2<=0||date2_2>28))flag_2=1;
                    else if(flagLeap_3==1&&(date2_2<=0||date2_2>29))flag_2=1;
                }
                else if(month2_2==3 &&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==4 &&(date2_2<=0||date2_2>30))flag_2=1;
                else if(month2_2==5 &&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==6 &&(date2_2<=0||date2_2>30))flag_2=1;
                else if(month2_2==7 &&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==8 &&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==9 &&(date2_2<=0||date2_2>30))flag_2=1;
                else if(month2_2==10&&(date2_2<=0||date2_2>31))flag_2=1;
                else if(month2_2==11&&(date2_2<=0||date2_2>30))flag_2=1;
                else if(month2_2==12&&(date2_2<=0||date2_2>31))flag_2=1;
            }
        if(flag_1==1||flag_2==1)
            {
                System.out.println(line2_1+"或"+line2_2+"中有不合法的日期.");
            }
        if(flag_1==0&&flag_2==0)
        {
            int flagJude=0;
            if(year2_2<year2_1)flagJude=1;
            else if(month2_2<month2_2)flagJude=1;
            else if(date2_2<date2_1)flagJude=1;
            if(flagJude==1)
            System.out.println(line2_2+"早于"+line2_1+",不合法!");
            if(flagJude==0)
            {
                    int yearDiff=year2_2-year2_1;
                int monthDiff=month2_2-month2_1;
                long dateDiff=dateD(line2_1,line2_2);
                System.out.println(line2_2+"与"+line2_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 line2_1,String line2_2)throws ParseException
    {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date start=dft.parse(line2_1);
        Date end=dft.parse(line2_2);
        Long starTime=start.getTime();
        Long endTime=end.getTime();
        Long num=endTime-starTime;
        return num/24/60/60/1000;
    }
}
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String str1 = input.nextLine();
        String a[] = str1.split("-");
        int a1[] = new int[a.length];
        for (int i=0;i<a.length;i++)
        {
            int number = Integer.parseInt(a[i]);
            a1[i] = number;
        }
        String str2 = input.nextLine();
        String b[] = str2.split("-");
        int b1[] = new int[b.length];
        for (int i=0;i<b.length;i++)
        {
            int number2 = Integer.parseInt(b[i]);
            b1[i] = number2;
        }
        int sum = (a1[0]-1)*365;
        sum += (a1[0]-1)/4;
        switch(a1[1])
        {
            case 12:
                sum+=30;
            case 11:
                sum+=31;
            case 10:
                sum+=30;
            case 9:
                sum+=31;
            case 8:
                sum+=31;
            case 7:
                sum+=30;
            case 6:
                sum+=31;
            case 5:
                sum+=30;
            case 4:
                sum+=31;
            case 3:
                if(a1[0]%400==0||a1[0]%100!=0&&a1[0]%4==0)
                {
                    sum+=29;
                }else
                {
                    sum+=28;
                }
            case 2:
                sum+=31;
            case 1:
                sum+=a1[2];
        }
        int sum2 = (b1[0]-1)*365;
        sum2 += (b1[0]-1)/4;
        switch(b1[1])
        {
            case 12:
                sum2+=30;
            case 11:
                sum2+=31;
            case 10:
                sum2+=30;
            case 9:
                sum2+=31;
            case 8:
                sum2+=31;
            case 7:
                sum2+=30;
            case 6:
                sum2+=31;
            case 5:
                sum2+=30;
            case 4:
                sum2+=31;
            case 3:
                if(b1[0]%400==0||b1[0]%100!=0&&b1[0]%4==0)
                {
                    sum2+=29;
                }else
                {
                    sum2+=28;
                }
            case 2:
                sum2+=31;
            case 1:
                sum2+=b1[2];
        }
        if(sum>sum2)
        {
            System.out.println("第一个日期比第二个日期更晚");
            System.out.println("两个日期间隔"+(sum-sum2)+"天");
            System.out.print("两个日期间隔"+(sum-sum2)/7+"周");
        }else
        {
            System.out.println("第一个日期比第二个日期更早");
            System.out.println("两个日期间隔"+(sum2-sum)+"天");
            System.out.print("两个日期间隔"+(sum2-sum)/7+"周");
        }
    }
}

这两题最大的问题其实是难度不高,但繁冗复杂,相当于食之无味却不能不吃。其他更多题目我就不多加赘述了,属于有一定难度,但不多。

踩坑心得:

不得不说,题目集1~3中有各种各样不完善的地方,那些测试点,有些明明没有问题却始终不能通过,只能完完全全按照规定的样子。

比如题目集1中的最后一题“判断三角形类型”中的判断等腰直角三角形的测试点,不能直接用三角形定理,不知道是计算机精度问题还是判断问题,最后用了给匪夷所思的办法才通过的测试点。而题目集1中的其他题目则没有特别的问题,只需要按部就班,按图索骥就能完成。

题目集2开始引入“菜单计价程序”,关于这个我可以讲个海枯石烂,“菜单计价程序1”还算可以接受,只要把输入的订单按要求分离并按不同的方法进行运算就行,而“菜单计价程序-2”就让人有点崩溃了,需要自己制定规则并运用规则,虽然看起来同根同源相差不大,但难度却是指数级增长,我遇到的最主要问题在于怎么存入菜品信息并按存入的信息运算,以及怎么删除已订的菜品,对于这点我给的方案是创建多个订单,分开操作,虽然运算量会增大,但能提供成功率。

当然,这些都不是唯一或唯二的问题,其他各种各样的问题也不能忽视,比如题目本身的繁冗复杂也就是我在上文中提到的题目集2中的“jmu-java-日期类的基本使用”和题目集3中的“判断两个日期的先后,计算间隔天数、周数”,这两个题目属于我看一眼题目及要求,写的欲望直接暴降至低谷的那种。题目集3中除开“菜单计价程序”大多数题目其实并不难,最多涉及一些专业化的知识,不过这是都是小问题,无伤大雅。

主要困难以及改进建议:

这三次题目集中最大的困难莫过于“菜单计价程序”了,对于这给题目可以说是绞尽脑汁了,不管是直接苦苦坚持,还是集思广益,都是收效甚微,完不成的还是完不成,当时最难受的莫过于运行失败后跳出来的一个个错误,对此我确实没有什么好的建议,不如说我自己都需要好的建议,只能说针对不同的题目不同的要求,要有不同的改进方向,并且在学习中多学多听多看吧。

总结:

讲了这么多,那我们到底从这三次题目集中学到了什么呢?在我看来我最大的收获是,在你要改进你的代码的时候千万要保存好修改前的代码!!!血的教训,在做重新做”菜单计价程序-3“的时候,我把”菜单计价程序-2“覆盖掉了,结果改完之后反而错误更多了,导致我浪费了很多时间精力。然后是我想提的意见,在做完实验后能不能把对应是实验范本发出来参考啊,像”菜单计价程序“这种一步一步逐渐递进的实验只要一下子落下了就永远追不回来了。以上,望诸君共勉。

标签:题目,String,int,PTA,else,Blog,flag,&&
From: https://www.cnblogs.com/hwjhelp/p/17426337.html

相关文章

  • oop题目集1~3的总结性Blog
    1)前言第一次作业:7-1主要是从控制台输入问题,和嵌套循环。7-2的要点是数据的精确度,严格的输出。7-3是数组的调用,循环的使用。7-4要点在与if和else的使用和标准的输入和输出。7-5主要是switch方法的使用7-6主要是substring的调用和字符串长度的判断length()和.equals()的使用7-7主要......
  • Blog
    前言 前三次的题目集,大概囊括了最基础的输入输出、类的创建;字符串的各种操作(定位某个特点字符在字符串中的下标、提取某段需要的子字符串、对于某个字符或某个子字符串的格式判断等等)、类的交互、函数的创建与使用以及正则表达式的运用等等。前两次的作业总体来说难度不大,对类的使......
  • 题目集1~3的总结
    1.前言在这三次的题目集中,主要涉及到了面向对象的相关知识点,包括类、对象、构造函数、封装、继承和多态等。我们学习了很多有关面向对象编程和软件设计的知识。这三次的题目集中,题目量和难度都有一定的增加。难度最大的题目集3中,我们需要设计一个完整的菜单计价系统,这一份题目集......
  • 前三次pta题目集分析
    (1)前言:我们是这个学期才开始学Java,上个学期学的是C,坦白说,我的C学的不好,然后这个学期的Java开始也是很吃力,编写代码很不熟练,这次pta的三个题目集,难度是在一步步增加的。第一次的题目一共是有9题,都算比较简单,知识点都是基础一些的计算等基本知识,我也拿到了满分。第二次的题目集......
  • 页面中调用swf 时allowScriptAccess 参数
    <paramname="allowScriptAccess"value="always"/>使用allowScriptAccess使Flash应用程序可与其所在的HTML页通信。此参数是必需的,因为fscommand()和getURL()操作可能导致JavaScript使用HTML页的权限,而该权限可能与Flash应用程序的权限不同。这与跨域安全性有......
  • PTA题目集1~3的总结
    (1)前言 题目集一总体来说题目量较多,但题目难度来说我个人觉得中等偏下,甚至可以说是很基础,因为上面的题目考察的知识点基本上来说就是java的基本语法使用,与c语言的语法大同小异,比如说数据类型的声明(包括浮点数的使用),循环语句,顺序语句,选择语句基本上都是互通的,我觉得要注意的就是......
  • 【NSSCTF逆向】【2023题目】《easy_re》《世界上最棒的程序员》《Check_Your_Luck》《
    题目easy_re解法很简单的一道题考的就是upx脱壳base64加解密拿到文件upx壳upx-d脱壳无壳放进ida很明显关键在于这个判断的两个字符串是啥。现在我们看看我们输入的s变成了什么。进入funcfunc的内容主要是对s进行操作然后给encode_这次我看明白了,这个很明显......
  • BLOG-1
    这是第一次写博客(之前也写了但是忘记存了,所以又要重新写了qwqqqqqqqq),这个博客会对题目集1-3进行总结。 前言 知识点:第一次题目集主要考察了从键盘输入(以及一些和包相关的知识,比如importjava.util.Scanner;的引入)和if...else语句;第二次题目集考察了我们第一次面......
  • BLOG-1
    前言知识点第一次作业:前八章Java语法相关内容。第二次作业:练习类的构造方法、方法的调用、参数传递、对象的构造与使用;练习循环结构;练习数据的输入与输出;理解抽象类与子类的关系。第三次作业:代码聚合性的调试。题量第一次作业:9题第二次作业:4题第三次作业:7题难度等情况第......
  • 六级作文题目汇总
    六级作文题目汇总2016上半年:议论文:现象解释livinginthevirtualworld.Trytoimaginewhatwillhappenwhenpeoplespendmoreandmoretimeinthevirtualworldinsteadofinteractingintherealworld.2016下半年:议论文:问题解决theimportanceofinnovationa......