| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11459 | Cheat Sheet |
|
| 11856 | Postfix Expression |
|
| 11933 | Vector Dot |
|
| 12224 | Doubly Linked List |
|
| 12320 | Count the Height |
|
| 12321 | The Cat Society 2 |
|
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
Description
Give a postfix expression, which has 4 operators, ‘+’ , ‘-’, '*' , '/',and positive integers. Print the result.

Notice: testcases don't include parentheses so just follow arithmetic rule.
And you don't need to consider divide 0 or some tricky rules : )
Input
The input contains exactly 1 sequences of postfix expression. It contains operators, ‘+’ ‘-’ '*' '/',and positive integers separated by a space.
The end of the input is 0.
The length of input sequence is smaller than or equal to 40
Output
The output is the result of the expression.
There is a newline symbol in the end
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Implement a Vector class that support n-dimensional vector dot product.
Your task is to complete
- const int operator[](int index) const;
- int operator*(const Vector& a);
inside the Vector class;
Remember to #include "function.h"!
Input
First line of input is an integer m, where m is the number of testcases.There are m testcases following.
A testcases consists of three lines:
- In the first line of each testcase, there are a string OP and an integer n, where OP is the operation and n is the vector size.
- In the second line of each testcase, there are n integers, representing all the elements inside the first vector, A.
- In the third line of each testcase, there are n integers, representing all the elements inside the second vector, B.
It is guaranteed that:
- 1 ≤ n, m ≤ 100
- For all the elements x in vector A and B, |x| ≤ 100
Output
For each testcase, according to its operation, output the result of A*B. There is a space after every number.
Sample Input Download
Sample Output Download
Partial Judge Code
11933.cppPartial Judge Header
11933.hTags
Discuss
Description
Maintain a doubly linked list, which supports the following operations:
(1) IH i : Insert a new integer i to the head of the list.
(2) IT i : Insert a new integer i to the tail of the list.
(3) RH : Print and remove the element in the head of the list. If the list is empty, print a blank line.
(4) RT : Print and remove the element in the tail of the list. If the list is empty, print a blank line.
(5) S : Swap the first floor(k/2) elements and the last k - floor(k/2) elements, where k is the total number of elements in the list. For example, if k = 5, the result of swapping a1, a2, a3, a4, a5 will be a3, a4, a5, a1, a2.
To improve the performance, it is suggested that three pointers be maintained in case4: head, tail and middle, where middle points to the floor(k/2) + 1-th element. With the help of these pointers, all of the operations can be done in constant time.
Input
There is only a single set of input and each operation will occupy a line. There will be a space between “IH” and “i”, and “IT” and “i”. The input contains OP operations, and it is terminated by end-of-file (EOF). You may assume that i is a nonnegative integer not greater then 100000. The list is initially empty.
For case1 and case2, 1<=OP<=1000
For case3, 1<=OP<=10000, For each S operation, O(n) is also accepted.
For case4 and case5, 1<=OP<=500000, each operation should be done in O(1).
Output
For each “RH” or “RT” operation, print a line containing the removed integer as commanded by the operation.
For RH and RT operation: if the list is empty, print a blank line.
Sample Input Download
Sample Output Download
Partial Judge Code
12224.cppPartial Judge Header
12224.hTags
Discuss
Description
Given a tree, count the height of the tree. Each node has unique integer identification (ID), but all IDs may not be consecutive
Hint :
Height of tree – The height of a tree is the number of edges on the longest path between that node and root.
Input
There are multiple test cases. Each test case begins with an integer N (1 <= N <=1000). In the following N lines, each line has two integers, a and b (1 <= a,b <= 1000), indicating node a and node b connected. The next line contains an integer R, which represents the root of the Tree. All the nodes may not be on the same tree, but you only need to output the tree rooted with R. The input is terminated by N = 0.
Output
Print the height of the tree (the number of edges on the longest path between that node and root) for each test case.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
2019/06/19 rejudge because invaild testcases
Wild cats take care of each other in the wild. However, when winter comes, the preys are not enough to feed all the cats. Therefore, the cats dine according to the order of their occupations. The order is as follows:
1. elder
2. nursy
3. kitty
4. warrior
5. apprentice
6. medicent
7. deputy
8. leader
In the tradition of the cat society, three different cats serve as the medicent, the deputy, and the leader respectively.
As for the other cats, except that the apprentices and kittys have the dining priority of the young over the old, for the other occupations, the old have higher priority. If the occupations and the ages of two or more cats are the same, they will dine in reverse lexicographical order according to their names.
Input
There are multiple test cases.
The first line of each test case contains two integers N and M, indicating the number of cats and the portions of food respectively, where 0<N,M<=10000.
The next N lines are the information of each cat, including name, occupation, and age.
The length of the names will not exceed 30 letters and will contain no spaces.
Output
Please output the cats that could eat the food in order, each name a line.