2987 - I2P(II)2024_Kuo_HW4 Scoreboard

Time

2024/04/16 15:10:00 2024/04/30 13:20:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team
1 13890 - Polynomial Calculator KuoTA We have noticed that the Function::parse function we provided has an error, that causes any constant to be casted into integer. Sorry for the confusion. . KuoTA 2024/04/29 19:57:43

# Problem Pass Rate (passed user / total user)
11443 3DShape
13131 Max-Heap Using Polymorphism
13440 Animal World
13890 Polynomial Calculator

11443 - 3DShape   

Description

Warning: You are not allowed to use malloc and free

Giving a bass-class Shape3D and 4 derived class : Sphere (球體), Cone (圓錐), Cuboid (長方體), Cube (立方體)

You need to calculate the volume of each shape.

PS:

V of Sphere: V = 4/3 π r3

V of Cone: V = 1/3 π r2 h

 

note : Remember to do some basic check, if the input is illegal (e.g.  length < 0, pi < 0 .....)  then the volume should be 0.

You don't need to consider the scenario like Cuboid -1 -2 3, volume would be 0 instead of 6.

Hint1: Be careful the type of volume is double.  4/3=1 (int),  so it should be 4.0/3.0

Hint2: You only need to implement the constructors.

Hint3: Note that Cube inherited Cuboid not Shape3D.

 

 

Input

There are only 4 shapes in this problem :

  • Sphere, following by its radius and pi. (e.g. Sphere 30 3.14)
  • Cone, following by its radius of bottom plane, height and pi. (e.g. Cone 3 100 3.14)
  • Cuboid, following by its length, width and height. (e.g. Cuboid 2 3 7)
  • Cube, following by its length. (e.g. Cube 2)

Output

Ouput the total volume of all the shapes.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11443.cpp

Partial Judge Header

11443.h

Tags




Discuss




13131 - Max-Heap Using Polymorphism   

Description

A. Definition of Max-Heap

A heap is a complete binary tree.

A max-heap is a complete binary tree in which the value in each internal node is greater
than or equal to the values in the children of that node.

B. Implementation of the Max-Heap Data Structure

1. Array:

An approach to store a max-heap is to use a single, contiguous block of memory cells, i.e., an array, for the entire tree. We store the tree’s root node in the first cell of the array. (Note that, for ease of implementation, we ignore the 0th cell and start from the 1st cell.) Then we store the left child of the root in the second cell, store the right child of the root in the third cell, and in general, continue to store the left and right children of the node found in cell in the cells 2n and 2n+1 respectively. With this technique, the tree shown below

would be stored as follows

2. Linked list:

We set a special memory location, call a root pointer, where we store the address of the root node. Then each node in the tree must be set to point to the parent and left or right child of the pertinent node or assigned the NULL value if there are no more nodes in that direction of the tree.

C. Method to push/pop an element

1. PUSH 

  • put the new element in the last place of the heap.
  • compare with the parent, swap them until the parent is greater or equal to the new element. 
  • example: push 11 to this heap

2. POP

  • put the last element to the top(root) of the heap.
  • compare with the greater child, swap them until all child is smaller than it. 
  • example: 

REQUIREMENTS:

Implement the constructor, push(), max(), pop() member functions of both the Array_MAX_HEAP and List_MAX_HEAP classes and deleteTree() of List_MAX_HEAP class.

 

Hint

For easy to solve this problem, you can call findparent(int cnt, ListNode *root) to return a ListNode which is the parent of node[cnt] in List_MAX_HEAP class.

Input

The first line contains an integer n.

Each of the next n lines has an instruction.

The instruction will be either:

  • A_push: push a new element ai into the array_max_heap.
  • L_push: push a new element ai into the list_max_heap.
  • max: show the max element. if the max-heap is empty, print -1.
  • A_pop: show and pop the max element in the array_max_heap. if the max-heap is empty, print -1.
  • L_pop: show and pop the max element in the list_max_heap. if the max-heap is empty, print -1.
  • size: show the size of the heap.

For all testcase:

1 <= n <= 1000, 0 <= ai < 231

(2/6) only A_push, L_push or size instruction

(2/6) only A_push, L_pushsize or max instruction

(2/6) contains all instruction

 

Output

For maxA_pop or L_pop instruction, print the max element in the heap.

For size instruction, print the size of the heap.

Remember to print \n at the end of output.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13131.cpp

Partial Judge Header

13131.h

Tags




Discuss




13440 - Animal World   

Description

There are N animals consisting of cats, fish, birds, and humans in a beautiful world.

Each animal has an unique name.

Initially, the HP of each animal is 0.

 

You have to perform the following operations:

 

  • Name <i>:Print the name of the i-th animal.
  • Species <i>:Print the species of the i-th animal.
  • HP <i>:Print the HP of the i-th animal.
  • Talk <i>:If the i-th animal is
    • human, print "Hello, world".
    • bird, print "Suba".
    • fish, print "?".
    • cat, print "Meow".
  • Breath <i> <x> :Increase the HP of the i-th animal by x.
  • Sleep <i> <x>:If the HP of the i-th animal is less than or equal to 100, multiply its HP by x.
    • (Note that its HP may become larger than 100 after this operation)
  • Eat <i> <j>
    • If the i-th animal is cat and the j-th animal is fish,
    • or the i-th animal is bird and the j-th animal is fish,
    • or the i-th animal is human and the j-th animal is fish or bird,
      • then increase the HP of the i-th animal by the HP of the j-th animal
      • and turn the HP of the j-th animal into 0.
    • If the i-th animal is human and the j-th animal is cat,
      • then turn the HP of the i-th animal into 0.
    • Otherwise, do nothing. 

 

Input

The first line of the input contains a number N Q — the number of animals in the world and the number of operations.

Each of the next i = 1, 2, ..., N lines contains two strings — the species and the name of the i-th animal.

The next Q lines is one of the operations described in the statement.

 

N ≤ 1000

Q ≤ 300000

1 ≤ x ≤ 10 in operation Breath and Sleep

1 ≤ i, j ≤ N in every operation

 

Output

Whenever there's an operation "Name <i>", print the name of the i-th animal.

Whenever there's an operation "Species <i>", print the species of the i-th animal.

Whenever there's an operation "HP <i>", print the HP of the i-th animal.

Whenever there's an operation "Talk <i>", print the corresponding sentences according to the problem statement.

Remember to print '\n' at the end of each line.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13440.cpp

Partial Judge Header

13440.h

Tags




Discuss




13890 - Polynomial Calculator   

Description

Warning: In Funciton::parse, the use of atoi for parsing constants makes the calculation different from what you'd get with a normal calculator, since atoi always returns an integer. Therefore, the testcases will be different than what you expect. Sorry for the confusion.

You will be given a preordered polynomial f(x). There will be Q queries, for each query x, please calculate the answer of f(x).

This is a partial judge problem, we have already implement a class Function to you. class Function has two functions:

  • static Funtion *parse(stringstream &ss):
    used to parse the input, and it's already implemented.
  • double eval(double x):
    used to calculate f(x), and this needs all its derived class to override it.

You need to implement the following 6 class inheritted from Function:

  • Constant: represent a constant value, ex. 1, 5.3 etc.
    • static Constant *create(double x):
      read a double x as the constant
  • Variable: represent variable, in this problem, the only variable is x.
    • static Variable *create(string s):
      read a string s as the variable
  • Polynomial: represent polynomial(xn), ex. 53, x7, 9x.
    • static Polynomial *create(Function *a, Function *b):
      read Function *a and Function *b as the base and exponential of the polynomial, which represent ab
  • Arithmetic: represent a arithmetic formula, ex. 5 + 3, 7 * x, x / 2.
    • static Arithmetic *create(Function *a, char op, Function *b):
      read Function *a, char op, and Function *b, the char op contains '+', '-', '*', '/'
  • Sin: represent a sine function sin(f(x)).
    • static Sin *create(Function *a):
      read Function *a, represents sin(a)
  • Cos: represent a cosine function cos(f(x)).
    • static Cos *create(Function *a):
      read Function *a, represents cos(a)

 

Please paste the following code after the code you write, and don't forget to include "function.h"!

Function* Function::parse(stringstream &ss){
    string s;
    ss >> s;
    if(s == "+" || s == "-" || s == "*" || s == "/"){
        Function *a = parse(ss), *b = parse(ss);
        Function *now = Arithmetic::create(a, s[0], b);
        return now;
    }else if(s[0] == 'x'){
        Function *now = Variable::create(s);
        return now;
    }else if(s == "**"){
        Function *a = parse(ss), *b = parse(ss);
        Function *now = Polynomial::create(a, b);
        return now;
    }else if(s == "sin"){
        Function *a = parse(ss);
        Function *now = Sin::create(a);
        return now;
    }else if(s == "cos"){
        Function *a = parse(ss);
        Function *now = Cos::create(a);
        return now;
    }else{
        Function *now = Constant::create(atoi(s.c_str()));
        return now;
    }
}

Input

The first line contains a preordered polynomial f(x):

  • Constants and Variable: constant value or variable x, ex. x, 5.3, 7
  • Polynomial operator: ** a b, represent ab
  • Arithmetic operator: op a b, represent a op b, ex. + 5 x means 5 + x. op constains only + - * /
  • Trignomotric operator: op a, represent op(a), ex. sin x means sin(x). op contains only sin and cos

The second line contains an integer Q, means the number of queries.

The following Q lines are the queries, each line contains a number x.

The length of the first line won't exceed 1e6, and 1 <= Q <= 2e5.

Each constant and query is between [-100, 100]. It's guarantee that no overflow and divided by 0

Output

For each x, please output the answer of f(x).

Sample Input  Download

Sample Output  Download

Partial Judge Code

13890.cpp

Partial Judge Header

13890.h

Tags




Discuss