1892 - I2P(I)2019_Hu_final_exam_makeup Scoreboard

Time

2020/01/14 16:00:00 2020/01/14 18:00:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
11459 Cheat Sheet
12462 Land Valuation System
12561 Equivalent relation 2
12578 Smart Thief

11459 - Cheat Sheet   

Description

Array.cpp
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      // for Arrays of different sizes, deallocate original
      // left-side Array, then allocate new left-side Array
      if ( size != right.size )
      {
         delete [] ptr; // release space
         size = right.size; // resize this object
         ptr = new int[ size ]; // create space for Array copy
      } // end inner if

      for ( size_t i = 0; i < size; ++i )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   } // end outer if

   return *this; // enables x = y = z, for example
} // end function operator=

// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
   if ( size != right.size )
      return false; // arrays of different number of elements

   for ( size_t i = 0; i < size; ++i )
      if ( ptr[ i ] != right.ptr[ i ] )
         return false; // Array contents are not equal

   return true; // Arrays are equal
} // end function operator==

// overloaded subscript operator for non-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
   // check for subscript out-of-range error
   if ( subscript < 0 || subscript >= size )
      throw out_of_range( "Subscript out of range" );

   return ptr[ subscript ]; // reference return
} // end function operator[]

// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
   // check for subscript out-of-range error
   if ( subscript < 0 || subscript >= size )
      throw out_of_range( "Subscript out of range" );

   return ptr[ subscript ]; // returns copy of this element
} // end function operator[]

// overloaded input operator for class Array;
// inputs values for entire Array
istream &operator>>( istream &input, Array &a )
{
   for ( size_t i = 0; i < a.size; ++i )
      input >> a.ptr[ i ];

   return input; // enables cin >> x >> y;
} // end function

// overloaded output operator for class Array
ostream &operator<<( ostream &output, const Array &a )
{
   // output private ptr-based array
   for ( size_t i = 0; i < a.size; ++i )
   {
      output << setw( 12 ) << a.ptr[ i ];

      if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
         output << endl;
   } // end for

   if ( a.size % 4 != 0 ) // end last line of output
      output << endl;

   return output; // enables cout << x << y;
} // end function operator<<

Inheritance type

Public

Protected

Private

Public data/ functions in the base class

Public in the derived class

Protected in the derived class

Private in the derived class

Protected data/ functions in the base class

Protected in the derived class

Protected in the derived class

Private in the derived class

Private data/fun in the base class

Not accessible in the derived class

Not accessible in the derived class

Not accessible in the derived clas

 
 
 
 
 
 

 

 

 

 

 

Polymorphism
  In the base class
  virtual void foo();
  In the derived class
  virtual void foo() override;

Abstract class and pure virtual function
  virtual void foo() =0;

Prefix/postfix operators
  complex &operator++();  // prefix
  complex operator++(int); // postfix

Operator overloading
  Binary operators
  /*1.non-static member function */
     complex operator+ (const complex&);
  /* 2. non-member function */
     friend complex operator* (const
                      complex&, const complex&);
  Unitary operators
  /*1.non-static member function */
      complex operator-();
  /*2. non-member function */
      friend complex operator~(const complex&);

Inheritance
  class A {
  // A is a base class }
  class B: public A {
  // B inherits A.
  // B is a derived class}

Dynamic allocation
  int *ptr = new int;
  delete ptr;
  int *ptr = new int[100];
  delete [] ptr;

String object
  #include<string>
  using namespace std;
  ...
  string str1 = "Hello";
  string str2 = "World";

Standard C++ Library - <string>

operator[]

For string a, a[pos]:

Return a reference to the character at position pos in the string a.

operator+=

Append additional characters at the end of current string.

operator+

Concatenate strings.

operator<

Compare string values.

For string a and b, a < b:

if a is in front of b in dictionary order, return 1, else return 0.

operator>

Compare string values.

For string a and b, a > b:

if a is in back of b in dictionary order, return 1, else return 0.

operator==

Compare string values.

For string a and b, a == b:

if equal, return 1, else return 0;

operator!=

Compare string values.

For string a and string b, a != b:

if not equal, return 1, else return 0;

compare()

Compare string.

For string a and b, a.compare(b):

if equal, return 0. If string a is in front of string b in dictionary order, return -1. If string a is in back of string b in dictionary order, return 1.

length()

Return length of string.

swap()

Swap string values.

push_back()

For string a, a.push_back(c):

append character c to the end of the string a, increasing its length by one.

 

Standard C++ Library - <sstream>

operator<<

Retrieves as many characters as possible into the stream.

operator>>

Extracts as many characters as possible from the stream.

str

Returns a string object with a copy of the current contents of the stream.

For example(1):
stringstream ss{“Hello”};    // constructor
string str = ss.str();    // a = “Hello”

For example(2):
stringstream ss;
ss<<”75”;
ss<<”76”;
int num;
ss>>num;    // num = 7576

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss




12462 - Land Valuation System   

Description

(modifyied by predix sum)

One day, the government want to know the total valuation of the land in rectangular area. As the member of the government, you decided to pick up this project and develop an query system. The example show below:

First, the government will give you each land's valuation with specific size 

Second, the government will give two point (top-left point and bottom-right point), and you need to compute the valuaiton of the land with specific area

ex1: (2,2) (3,3) ==>  the valuation of land is 10 + 20 + 4 + 6 = 40

ex2: (1,1) (2,2)  ==>  the valuation of land is 10 + 20 + 5 + 10 = 45

ex3: (1,3) (3,3)  ==>  the valuation of land is 30 + 20 + 6 = 56

Hint : If you get TLE for last two testcases, you should think how to decrease some redundant operations for calculations (you can try to expand 1D prefix sum to 2D prefix sum)

Note that: If you are using visual studio, add #pragma warning(disable:4996) in the first line so that you can use scanf on your local machine.

Input

M N

S11  S12 . .. S1N

...

SM1 SM2 ...SMN

T

pma_1 pna_1 pmb_1 pn b_1

pma_1 pna_2 pmb_2 pn b_2

....

pma_T pna_T pmb_T pn b_T

 

pm1_1 pn1_1 pm2_1 pn 2_1

M : the row size ( 2 < M < 1000)

N:  the column size (2 < N < 1000)

Sij: the element in the 2D array (0 <= Sij < 100)

T : the times of query (2 <= T < 5000)

pma_i pna_i pmb_i pnb_i : the top-left point and bottom-right point

( 1 <= pma_i <= pmb_i <= M)

( 1 <= pna_i <= pnb_i <= N)

Output

Compute the valuaiton of specific land area (followed by a newline character at the end of  each output answer).

Sample Input  Download

Sample Output  Download

Tags




Discuss




12561 - Equivalent relation 2   

Description

There are N integer pointers, indexed from 0 to N-1 (N<100). Each pointer initially points to an integer of value 0.

There are three kinds of instructions. The instruction “S n k” is used to set the integer, which pointer n points to, to be k. For example, S 1 10 means that the integer that pointer 1 points to is set to 10. And the instruction “P n m” means that pointer n points to the integer that pointer m points. For example, P 2 1 means that pointer 2 points to the integer that pointer 1 points to. After P 2 1, pointer 2 and pointer 1 point to the same integer, which is pointed by pointer 1. The Instruction "M" is used to move all pointer to point the next pointer, and the last pointer will point to the first pointer before the first pointer point to the next pointer. 

The instruction 'M' :

Take 3 elements for example:

before M : pointer0 -> integer 10, pointer1 -> integer 20, pointer2 -> integer30.  output : ==> 10 20 30

after M: pointer0-> integer 20, pointer1 -> integer30, pointer2 -> integer 10. output ==> 20 30 10

Note that you don't have to change all the pointers if one pointer changes its target. The following table is an example. The instructions are P 1 2 and then P 2 3.  You do not have to change the target of pointer 1.

instruction

Description

P 1 2

Pointer 1 points to the integer that pointer 2 points to.

 

P 2 3

Pointer 2 points to the integer that pointer 3 points to.

And you don’t have to change the target of pointer 1.

Finally, output all the values that pointers 0 to N-1 point to in order.

Note that

1.      This problem involves three files.

  • function.h: Function definition of execInstruct.
  • function.c: Function describe of execInstruct.
  • main.c: A driver program to test your implementation.

You will be provided with main.c and function.h, and asked to implement function.c.

2.     For OJ submission:

       Step 1. Submit only your function.c into the submission block. (Please choose c compiler) 

       Step 2. Check the results and debug your program if necessary.

Hints:

main.c

#include <stdio.h>
#include "function.h"

#define SIZE 100

int main() {
        int *ptrArr[SIZE];
        int dataArr[SIZE] = {0};
        char inst;
        int dataNum, instNum;
        int param1, param2;
        int i;

        /* input */
        scanf("%d %d", &dataNum, &instNum);

        /* initialize the ptrArr */
        for (i = 0; i < dataNum; i++)
                ptrArr[i] = &dataArr[i];

        for (i = 0; i < instNum; i++) {
                scanf(" %c", &inst);
                if(inst == 'M'){
                        execInst(ptrArr, inst, -1, -1, dataNum);
                }else{
                        scanf("%d %d",&param1, &param2);
                        execInst(ptrArr, inst, param1, param2, dataNum);
                }
        }

        /* output */
        for (i = 0; i < dataNum - 1; i++) {
                printf("%d ", *ptrArr[i]);
        }
        printf("%d", *ptrArr[i]);

        return 0;
}

function.h

#ifndef FUNCTION_H
#define FUNCTION_H

void execInst(int *ptrArr[], char inst, int param1, int param2, int dataNum);

#endif

Input

The first line contains two positive X and Y. X indicates the size of data. Y indicates that there are Y instructions needed to be done.

The next Y lines contain the instructions.

Note:

Test case 1 - 5 will not contain the new instruction 'M'

Test case 6-7 will contain the new instruction 'M'

Output

All the values that pointers 0 to pointer N-1 point to in order. Each value is seperated by a blank ' '.

# Note that there is no '\n' at the end of the output.

Sample Input  Download

Sample Output  Download

Partial Judge Code

12561.c

Partial Judge Header

12561.h

Tags




Discuss




12578 - Smart Thief   

Description

You are a smart thief who broke into sombody's house and found all the valuables.

You know the value of each item in the house, but kind as you are decided to take as few items as possible, as long as the sum of values you take is strictly greater than the sum of values of the remaining valuables.

Determine the number of items you will take.

Input

N

V_1 V_2 ... V_N

(N is a positive integer not exceeding 5000, and each V_i is a positive integer not exceeding 100000)

Output

The number of items to take, as an integer, followed by a newline character

Sample Input  Download

Sample Output  Download

Tags




Discuss