| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 10993 | Polynomial class |
|
| 10996 | Josephus with Fibonacci number |
|
Description
Create a class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the term 2x4 has the coefficient 2 and the exponent 4.
- Use a 51-element array, coefficients, of digits to store each coefficient.
- Use a integer variable, greatestPower, to store an exponent.
Provide public member functions that perform each of the following tasks:
- Adding two Polynomial.
- Subtracting two Polynomial.
- Multiplying two Polynomial.
- Printing coefficients of the Polynomial in descending order.
Note:
1. This problem involves three files.
- function.h: Class definition of Polynomial.
- function.cpp: Member-function definitions of Polynomial.
- main.cpp: A driver program to test your class implementation.
You will be provided with main.cpp, and asked to implement function.h and function.cpp.
function.h
main.cpp
2. For OJ submission:
Step 1. Submit only your function.cpp into the submission block.
(***Note that you don’t need to submit your function.h.)
Step 2. Check the results and debug your program if necessary.
Input
There are four lines.
The first two lines represent the greatest power and the corresponding coefficients of the first polynomial.
The last two lines represent the greatest power and the corresponding coefficients of the second polynomial.
Note that the coefficients are in descending order and each element is separated by a blank.
Output
Your program should print the coefficients of the sum, difference and product of these two polynomials in descending order, respectively.
Note that every answer should be followed by a new line character.
Sample Input Download
Sample Output Download
Partial Judge Code
10993.cppPartial Judge Header
10993.hTags
Discuss
Description
The Josephs problem is notoriously known. For those who are not familiar with the problem, among n people numbered 1, 2, . . . , n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give the message about the incident.
The persons are eliminated in a very peculiar order; m is a dynamical variable, which each time takes a different value corresponding to the Fibonacci numbers succession (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...). So in order to kill the i-th person, Josephus counts up to the i-th Fibonacci number.
For example, there are 6 people in a circle, and the sequence of counting is Fibonacci number succession (1, 1, 2, 3, 5 …).
In the beginning, the step to kill m = 1. The sequence of killing people is as follows.
1.............................(kill 1, and m is changed to 1)
2.............................(kill 2, and m is changed to 2)
3, 4.........................(kill 4 ,and m is changed to 3)
5, 6, 3.....................(kill 3 ,and m is changed to 5)
5, 6, 5, 6, 5.............(kill 5)
Then print 6 as answer.
Let’s solve this problem using C++. You have been provided with the following class definitions:
class Node
{
friend class Josephus;
public:
Node():next( NULL ){
}
Node( const int &info ) //constructor
:number( info ), next( NULL )
{
} //end ListNode constructor
private:
Node *next;
int number;
};//end class Node
class Josephus
{
public:
Josephus();
~Josephus();
Josephus(const int &);
int kill(); // return the survival’s position
private:
void generatecircularlinkedList(const int &); // generate circular linked-list
void generateFib(const int &); // generate a Fibonacci sequence table
int sequence[50]; // store Fibonacci number
int noOfPeople;
Node *head;
};
REQUIREMENTS:
In this practice, you are asked to implement the following member functions:
Josephus class:
- constructor
- destructor
- int kill();
- void generatecircularlinkedList(const int &);
- void generateFib(const int &);
Note:
1.This problem involves three files.
- function.h: Class definitions.
- function.cpp: Member-function definitions.
- main.cpp: A driver program to test your class implementation.
You will be provided with main.cpp and function.h, and asked to implement function.cpp.
function.h
main.cpp
2.For OJ submission:
Step 1. Submit only your function.cpp into the submission block.
Step 2. Check the results and debug your program if necessary.
Input
Each line contains a number n<=45, which is the number of people. Input is terminated by EOF.
Output
The output will consist in separate lines containing the position of the person which life will be saved.