首页 > 其他分享 >BLOG-1

BLOG-1

时间:2023-05-24 20:01:21浏览次数:32  
标签:菜单 int 31 BLOG year Dish new

  这是第一次写博客(之前也写了但是忘记存了,所以又要重新写了qwqqqqqqqq),这个博客会对题目集1-3进行总结。


 

  • 前言   
    •  知识点:第一次题目集主要考察了从键盘输入(以及一些和包相关的知识,比如import java.util.Scanner;的引入)和if...else语句;第二次题目集考察了我们第一次面对对象编程(菜单计价程序),以及第四题dp(动态规划);第三次题目集对对象更加复杂的处理(菜单计价程序升级)以及HashSet容器和将输入数据以字符串的形式读取再转换成想要的数据(以此减少读取的耗时)
    • 题量 :还可以
    • 难度:第一次题目集特别简单,但是到了第二次就很有难度了,首先是菜单计价程序的出现直接让我人傻了,然后就是第二次的第四题为什么会出一道dp在那里我现在都想不明白(而且L老师上课还讲了这道题怎么写,但是!!!他的做法是写五个还是几个循环,反正这么写是肯定不能通过全部的测试点的),第三个除了菜单以外其他题目都还可以,只要去学习一下HashSet去重容器基本上就可以过了,但是菜单真的让人一言难尽,记得讲题目集二的时候,让L老师讲一下怎么写菜单,他讲了个大象装进冰箱的例子并且用epl带着我们写了一遍(也不能说是带着吧看着他写),他说这个例子比菜单难多了,那个大象的我是真的听懂了,然后回去还是不会写菜单,之后就再也没有讲过题目了(除了上周讲期中题目讲了一下,但是那时已经不能阻止我的PTA菜单连续低分的惨状了)
  • 设计与分析
    • 菜单1
    1. 类图

                                                                          

                                    (为什么他只有一个类是因为这道题目很简单,不需要代点单或者删除订单什么的,所以所以最后我发现我只写了一个子类orz)

      2.源代码

      

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long sum = 0;
        Dish[] dd = new Dish[4];
        dd[0]=new Dish("西红柿炒蛋",15);
        dd[1]=new Dish("清炒土豆丝",12);
        dd[2]=new Dish("麻婆豆腐",12);
        dd[3]=new Dish("油淋生菜",9);
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        while(a.indexOf("end")==-1)
        {
            String[] t = a.split(" ",2);
            int i;
            for(i =0;i <4;i++)
            {
                if(t[0].equals(dd[i].name))
                        break;
            }
            if(i==4)
            {
                System.out.println(t[0]+" does not exist");
            }
            else
            {
            int n = Integer.valueOf(t[1]);
            if(n==1)
                sum = sum + dd[i].price;
            if(n==2)
                sum = sum + Math.round(dd[i].price*1.5);
            if(n==3)
                sum = sum + dd[i].price*2;
            }
            a = in.nextLine();
        }
        System.out.println(sum);
    }

}
class Dish
{
    String name;
    int price;
    Dish(String name , int price)
    {
        this.name = name;
        this.price = price;
    }
}
    •   菜单2-3( 因为菜单3是在菜单二基础上的升级,所以接下里我会直接贴出菜单三相关类图以及代码)
    1. 类图

 

 

 

 

 

 

     2.代码

     

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 类名称 对象数组名[] = new 类名称[长度];
        Dish d[] = new Dish[2];
        d[0] = new Dish("麻婆豆腐", 12);
        d[1] = new Dish("油淋生菜", 9);
//error:                                        d[0].name="麻婆豆腐";d[0].unit_price=12;
//                                              d[1].name="油淋生菜";d[1].unit_price=9;
//text:                                         System.out.println(d[1].name+" "+d[1].unit_price);
        Scanner scanner = new Scanner(System.in);
        String useless = scanner.nextLine();
        int din = 0;
        int tab = 0;
        String[] re = useless.split(" ", 2);
        int pricc = Integer.valueOf(re[1]);
        if (pricc >= 400)
            System.out.println(re[0] + " price out of range 400");
        else {
            d[din] = new Dish(re[0], pricc);
            din++;
        }
        useless = scanner.nextLine();
        String[] re1 = useless.split(" ", 3);
        pricc = Integer.valueOf(re1[1]);
        if (pricc >= 400)
            System.out.println(re1[0] + " price out of range 400");
        else {
            d[din] = new Dish(re1[0], pricc);
            din++;
        }
        String a = scanner.nextLine();// ***
        while (a.indexOf("end") == -1) {
            String[] result1 = a.split(" ", 4);
            if (a.indexOf("table") == -1) {
                System.out.println("wrong format");
                return;
            }
            String[] time = result1[3].split("/");
            int hour = Integer.valueOf(time[0]);
            int min = Integer.valueOf(time[1]);
            int se = Integer.valueOf(time[2]);
            String[] time1 = result1[2].split("/");
            int year = Integer.valueOf(time1[0]);
            int month = Integer.valueOf(time1[1]);
            int day = Integer.valueOf(time1[2]);
            double sale = 0;
            int dayy = 0;
//            String myString = "hello123"; //创建一个字符串变量myString
            char[] my; // 创建一个字符数组myCharArray
            my = result1[1].toCharArray(); // 将字符串变量转换为字符数组
//            for(int i=0; i<myCharArray.length; i++) {
//                System.out.print(myCharArray[i] + " "); //将每个字符打印出来
//            }
            if (my[0] >= 'a' && my[0] <= 'z') {
                System.out.println("wrong format");
                return;
            }
            if (my[0] >= 'A' && my[0] <= 'Z') {
                System.out.println("wrong format");
                return;
            }
            int number = Integer.valueOf(result1[1]);
            if (result1[3].length() != 8 || number > 55 || number <= 0) {
                System.out.println("wrong format");
                return;
            }
            if (judge(year) == 0) {
                System.out.println("not a valid time period");
                return;
            }
//             if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
//                 sale = onsale(hour * 3600 + min * 60 + se);
//             else
            sale = 1.00;
            System.out.println(result1[0] + " " + result1[1] + ": ");
            a = scanner.nextLine();
            int i = 0;
            int s = 0;
            Order o[] = new Order[1000];
            int p1 = 0;
//         Order p1 = new Order();
            int p = 0;
            int[] cc = new int[100];
            int cindex = 0;
            while (a.indexOf("end") == -1 && a.indexOf("table") == -1) {
                if (a.indexOf("delete") != -1) {
                    int x = Integer.valueOf(a.substring(0, 1));
                    cc[cindex] = x;
                    cindex++;
                    int flag = 0;
                    if (x == 0) {
                        System.out.println("niho");
                    }
                    for (int db = 0; db < cindex - 1; db++) {
                        if (cc[db] == x) {
                            System.out.println("deduplication " + x);
                            flag = 1;
                            break;
                        }
                    }
                    if (x > i)
                        System.out.println("delete error;");
                    if (x <= i && flag == 0)
                        s = s - o[x - 1].price;
                } else {
                    String[] result = a.split(" ", 4);
                    int j;
                    for (j = 0; j < din; j++) {
                        if (a.indexOf(d[j].name) != -1) {
                            p = d[j].unit_price;
                            break;
                        }
                    }
                    if (j == din)
                        System.out.println(result[1] + " does not exist");
                    else {
                        int n = Integer.valueOf(result[2]);
                        int m = Integer.valueOf(result[3]);
                        int juu = 0;
                        if (n == 1)
                            p1 = p * m;
                        if (n == 2) {
                            double loss = p * 1.5 * m;
                            // double loss = p*1.5*m;
                            p1 = (int) (loss + 0.5);
                        }
                        if (n == 3)
                            p1 = p * 2 * m;
                        o[i] = new Order(result[1], p1);
                        if (n >= 4) {
                            System.out.println(n + " portion out of range 4");
                            juu = 1;
                        }
                        if (m > 15) {
                            System.out.println(result[0] + " num out of range 16");
                            juu = 1;
                        }
                        if (juu == 0) {
                            s = s + p1;
                            System.out.println(result[0] + " " + result[1] + " " + p1);
                        }
                        i++;
                    }
                }
                a = scanner.nextLine();
                if (a.indexOf("table") != -1) {
                    tab = 1;
                    break;
                }
            }
            if (sale != 0) {
                System.out.print(result1[0] + " " + result1[1] + ": ");
                System.out.print(s + " ");
                double sxd = s * sale * 1.0;
                if (Prt(year, month, dayy) >= 1 && Prt(year, month, dayy) <= 5)
                    sxd = sxd * 0.7 + 0.5;
                s = (int) sxd;
                System.out.println(s);
            } else
                System.out.println(result1[0] + " " + result1[1] + " out of opening hours");
        }
    }

    private static int judge(int year) {
        // TODO Auto-generated method stub
        if (year < 2022 || year > 2023)
            return 0;
        return 1;
    }

    public static int Prt(int year, int month, int day) {
        int[] x = new int[12];
        x[0] = 31;
        x[1] = 28;
        x[2] = 31;
        x[3] = 30;
        x[4] = 31;
        x[5] = 30;
        x[6] = 31;
        x[7] = 31;
        x[8] = 30;
        x[9] = 31;
        x[10] = 30;
        x[11] = 31;
        if (LeapYear(year) == 1)
            x[1] = 29;
        int s1 = 0;
        for (int i = 0; i < month - 1; i++)
            s1 = s1 + x[i];
        s1 = s1 + day;
        int c;
        // c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
        c = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + s1) % 7;
        // Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
        if (c == 0)
            c = 7;
        return c;
    }

    public static int LeapYear(int year) {
        if (year % 4 == 0 && year % 100 != 0)
            return 1;
        if (year % 400 == 0)
            return 1;
        return 0;
    }

    public static double onsale(int x) {
        if (x >= 61200 && x <= 73800)
            return 0.8;
        if (x >= 37800 && x <= 52200)
            return 0.6;
        return 0;
    }
}

class Dish {
    String name;
    int unit_price;

    public Dish() {
    }

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

class Order {
    String name;
    int price;

//     public Order{
//     }
    public Order(String name, int price) {
        this.name = name;
        this.price = price;
    }
}

class Table {

Order order;

int num;

int year;

int month;

int day;

int hour;

int min;

int sec;

int week;

public Table(){

 

}

public boolean check(){

int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (month >= 1 && month <= 12) {

//判断是否为闰年

if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) {

if (month == 2 && day <= 29) return true;

else {

if (day <= days[month - 1]) return true;

}

} else {

if (day <= days[month - 1])

return true;

}

}

return false;

}

boolean checkorder(int x){

   class Order {

        Record[] records = new Record[100];//保存订单上每一道的记录
        int i=0,s=0;//s为点菜数
        int year;
        int month;
        int day;


        public Order(){

        }


        int getTotalPrice(){
            int num = 0;
            for(i=0;i<s;i++){
                if(records[i].life==0)
                     num = num + records[i].getPrice();
            }
            return num;
        }//计算订单的总价
        int ygetTotalPrice(){
            int num = 0;
            for(i=0;i<s;i++){
                if(records[i].life==0)
                    num = num + records[i].ygetPrice();
            }
            return num;
        }//计算订单的原总价

        public Record addARecord(int orderNum,String dishName,int portion,int num,Menu menu,int week,String name ,String tel){

            for (i=0;i<s;i++){
                if(dishName==records[i].d.name&&portion==records[i].portion) {
                    records[i].num = records[i].num + num;
                    s++;
                    return records[i];
                }
            }
            if(i==s) {

                records[i] = new Record();
                if(menu.searthDish(dishName)!=null) {
                    records[i].d = menu.searthDish(dishName);
                    records[i].orderNum = orderNum;
                    records[i].d.name = dishName;
                    records[i].portion = portion;
                    records[i].num = num;
                    records[i].week = week;
                    records[i].name = name;
                    records[i].tel = tel;
                    s++;
                }
                else {
                    records[i].d = new Dish();
                    records[i].d.name=dishName;
                    records[i].life=6;//订单的菜不存在
                    s++;
                }
                return records[i];
            }
            return null;
        }//添加一条菜品信息到订单中。

        public void delARecordByOrderNum(int orderNum){
            for (i=0;i<s;i++){
                if(records[i].orderNum==orderNum){
                    if(records[i].life==1)
                        records[i].life = 2;
                    else
                        records[i].life = 1;
                }

            }

        }//根据序号删除一条记录

        public int findRecordByNum(int orderNum){
            for (i=0;i<s;i++){
                if (records[i].orderNum==orderNum)
                    return i;
            }
            return -1;
        }//根据序号查找一条记录

    }

 

for(int j=0;j<=order.s-1;j++){

if(x<=order.records[j].orderNum)

return false;

}

return true;

}

 

}

 

 

  •  踩坑心得
    • 非菜单中的踩坑:
    1. 在题目集二中一开始我认为只要算出来几月几日是这一年的第多少天然后再用这个第多少天%7的结果就是星期几,后来我发现有测试点一直过不了,然后翻了下日历才发现原来不是每年的1月1日都是星期一...改进方法是去csdn了搜了一下得了个简单的计算公式:

           c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D

           Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。

     2.在题目集三中(7-3 去掉重复的数据),有一个测试点死活过不了,后来才知道是因为一个数据一个数据读太耗时了,最后选择将整个字符串读下来再进行操作解决了该问题

                  Scanner sc = new Scanner(System.in);

            String str = sc.nextLine();

            String[] arr = str.split(" ");

     3.另外就是封装性那道题目,因为上学期写C语言PTA函数题的影响(上学期写的函数题都是主体题目给了,只需要在PTA里补写函数体就可以了)导致我把所有类都写好了还是过不了,最后才发现是要把题干给的主体复制过去才行

    • 菜单中的踩坑:

    先看一下图片就知道踩了多少坑了(其实就是不太会写)

 

                                             (总而言之就是一片绿)

      1.可以看到一开始题目集2菜单我写的十分简单(只用到了Dish类,甚至极端点可以连Dish类都不写上)导致我后面根本没办法想出来为什么非要有Menu类,Table类(因为感觉有Dish,Record,Time,Order类就足够了)

      2.没有对字符串做解析,这个问题是我后面(菜单四)才意识到的,导致代码很混乱,很没用条理,而且没有办法通过一些测试点(比如混乱输入之类的),我写的就是一个大循环读到end结束下套个小循环,小循环读到没有Table结束,出来就是读点菜,读到有delet就进行删除操作,但是这么写的话有一个以上的Table就会很难用

      3.折扣的计算十分混乱,我一开始用的方法是这样的,给一个乘以折扣(或者中份乘以1.5)的数据+0.5,再对他强制类型转换,这样小数位大于等于5的就会进一,反之则不进(但是这样写很麻烦不说,我菜单一还因为这么写过不了一个测试点),现在回头看看,为什么当初我不用Math.round()....

  • 主要困难以及改进建议
    • 困难:主要的困难就是来自于菜单,菜单要一口气写出一个体系,可以过所有测试点的体系,但是不是所有测试点都是已知,而且对输入数据要求十分苛刻(有很多测试点都是特殊例子或者不按规定的输入)导致要考虑的特殊情形过多,并且菜单在PTA结束后也没有标准题解或者讲解,导致后面菜单4-5更无从下手
    • 改进建议:
      •   于我自己:应该多花时间在PTA作业上,这样不至于最后几天赶菜单来不及导致分数过低,应该注意代码简洁,能写类的尽量写类,能写方法的尽量写方法,对数据处理要敏感,对特殊情形处理要有条理按次序
      •   于老师:应该及时讲解题目或者发布相关测试点或标准题解,这样不至于让学生做完一次下一次还是这么差,我做了这么多菜单但是因为一直一知半解所以除了低分外更是一点进步没有,其次,PTA发布题目大多与所学知识没有关联,比如讲了抽象类应该让同学做几道相关的题目进行巩固而不是继续发布菜单升级版。还有,比如去重容器set,dp动态规划这些根本在课上都从未涉及过,不知道为什么要出现在PTA题目集中
  • 总结

      其实上面该总结的都总结的差不多了,最后还要说的话就是自己在java上面花的时间其实不算多,上课其实听的还挺认真的,但是下课回去因为没有相关练习,自己也不会主动去把上课那些东西再敲一遍,导致其实相关知识掌握的如何我自己都没有什么把握,对“面对对象编程”其实还是不太了解其内核本质,这样就比较可怕了,java比上学期c语言要难的多,但是更加智能,用起来更加方便,而我花点时间却远不如上学期那般,其中有各种原因而搁置了相关的学习,还是要踏踏实实去做的未来!!!

      要说学到了什么,上课学到了很多,CSDN上学到了很多,但从PTA(主要是菜单)中其实没有多大收获,(反而最大的收获还是那道时间题,写出来高兴了很久),可能我一开始也不是很想写菜单把(毕竟刚做完C语言课设不久),归根结底还是态度上有问题,应该改正。

标签:菜单,int,31,BLOG,year,Dish,new
From: https://www.cnblogs.com/rrringo/p/17426701.html

相关文章

  • BLOG-1
    前言知识点第一次作业:前八章Java语法相关内容。第二次作业:练习类的构造方法、方法的调用、参数传递、对象的构造与使用;练习循环结构;练习数据的输入与输出;理解抽象类与子类的关系。第三次作业:代码聚合性的调试。题量第一次作业:9题第二次作业:4题第三次作业:7题难度等情况第......
  • PTA 1—3次题目集总结 Blog1
    一.前言前三次题目集总的来说知识点很多,题量也很大,除了第一次题目简单,第二三次题目的难度跨度太大了,第一次都是很基础的题目,第二三次题目难度突然提高很多,措不及防,完成得很困难,由于菜单计价系统是第一次写,难度很大,完成的不太好。二.设计与分析第一次题目集:总的来说,第一次题目......
  • PTA题目集1~3的总结性Blog
    一、前言:我通过学习面向对象程序设计这门课程,深入掌握了Java语言的知识。截至目前,三个PTA作业,这些作业主要涉及Java的结构、类的运用、以及一些方法的使用,其中类的应用是重点。这三次作业的难度逐渐加大,同时作业量也在逐步增加。最令我印象深刻的是点菜,每一次都让我心如焦土,无可......
  • BLOG-1
    一、题目集分析本次博客将对PTA的前三次题目集进行分析,情况如下:(1)题量分析:第一次练习共计9题第二次练习共计4题第三次练习共计7题从题量而言,数量适中。(2)知识点分析:第一次题目集更侧重于编程基础,主要涉及基本数据类型,也涉及部分引用类型,其次还多用到流程控制当中的if......
  • blog1
    前言    菜单类题目之外的其他的小题目都还好,差不多都做出来了,在做这些题目的同时我也学会了很多java语言的知识,明白了合理用JAVA中包含的自带的函数是一种便捷高效的方法 显而易见,此三次的PTA作业所考察的知识点以及难度呈现不断变大的趋势。PTA1作业难度简单,虽然题目量......
  • 数字名片工具 BBlog:使用一个链接,快速创建和分享你的信息主页和数字花园
    数字名片BBlog:使用一个链接,快速创建和分享你的信息主页和数字花园随着移动互联网技术的快速发展,数字名片产品已成为现代社交和网络营销的重要工具。数字名片可以帮助个人和企业在各种场合中展示和分享联系信息,同时还具有便捷、环保、易于管理等诸多优点。在本文中,我们将介绍......
  • xxxxhttps://blog.csdn.net/holecloud/article/details/80139297
    #include"stdafx.h"#include<opencv2\imgproc\imgproc.hpp>#include<windows.h>#include<opencv2/opencv.hpp>#include<cmath>#include<iostream>//#include"DetectPackage.h"#include<cmath>usin......
  • BLOG-1 (PTA 1~3)
    前言在本阶段的三次题目集中,我们主要涉及了菜单计价程序的设计与实现。这些题目涵盖了点菜订单的处理、菜品价格计算、代点菜功能以及总价的计算等方面的内容。通过完成这些题目,我们可以加深对Java编程语言的理解,熟悉面向对象的设计思想,并应用这些知识解决实际问题。设计与分析......
  • 题目集 1~3 的总结性 Blog
    前言:题目集1~3的知识点、题量、难度等情况如下:知识点:JAVA基础,基础算法,面向对象程序设计题量:共计3道题目难度:题目从易到难,分为三个层次,分别为入门、中等和高级设计与分析:本次Blog重点分析菜单计价系列题目,即题目集2的7-1、7-2以及题目集3的7-1。这三......
  • Blog说明
    较新的博客中的代码会删除所有的省源,namespace,快读和宏定义等CF的题解有时会删去主程序这里给出删除部分,由于本人模板的更新,不保证加上之后的代码可以通过#include<cstdio>#include<cmath>#include<iostream>#include<cstring>#include<algorithm>#include<queue>......