1258. It
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
As an unnamed space agency endured the ridicule following their now-infamous metric/English unit conversion, they made a vow to never let that error happen again. Unfortunately, they over-hired poor college students with excellent metric/English unit conversion skills but not calculus skills. In fact, nobody at this agency can now remember how to calculate the derivative of a polynomial, let alone evaluate the derivative of a polynomial for a given value of x.
As any good government agency would do in this case, they've decided to outsource the task...to you. And not only do they want you to do the work, they want you to show how you did each step of your calculation so that they might re-learn the skill.
Input
Input to this problem will begin with a line containing a single integer n indicating the number of input polynomials. Each of the following n lines contain:
- a value of x at which the first derivative of the polynomial is to be evaluated;
- and the polynomial.
All polynomials are expressed in terms of x, with single-digit integer coefficients (positive or negative) and non-negative single-digit integer powers of x. Polynomials do not contain spaces. Coefficients and powers of 1 (and powers of 0) are omitted from the notation, as is any "+" sign that might apply to the leading term. The leading term has the highest order, and is followed by the one (if any) with the next-highest order and so on.
Output
For each polynomial in the input, output the following lines:
- The string "POLYNOMIAL N" where N is 1 for the first polynomial, 2 for the second, etc.;
- The polynomial, exactly as in the input;
- The first derivative of the polynomial from line 2;
- The polynomial in line 3 with x replaced by its value in parenthesis (e.g., if x is 6, "2x^3" becomes "2(6)^3");
- The polynomial in step 4 with each term fully evaluated;
- and the resulting integer, which is equivalent to f'(x) evaluated at the given value of x.
Sample Input
3 5 2x^7+x^2+3x-9 0 -5x^7+x^3+1 -3 5
Sample Output
POLYNOMIAL 1 2x^7+x^2+3x-9 14x^6+2x+3 14(5)^6+2(5)+3 218750+10+3 218763 POLYNOMIAL 2 -5x^7+x^3+1 -35x^6+3x^2 -35(0)^6+3(0)^2 0+0 0 POLYNOMIAL 3 5 0 0 0 0
好凶残的模拟题,我的思路是:
首先预处理,省略一的都补上,常数项加上x^0,为了后面的读取方便
然后读取,读到一个结构体数组里面
处理即可:
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <cmath>
using namespace std;
bool is_end; //if the input are 2 constant, just output easily and end it
int x_v; //the value of the x
struct term { // the struct to store a term
int pol; //coefficient
int pow; //power
term(int p1, int p2) {
pol = p1;
pow = p2;
}
};
vector<term> t;
void input() {
string temp;
cin >> x_v >> temp;
//output 1
cout << temp << endl;
//preprocessing
if (temp[0] != '-') { //to complete the positive num with a '+'
temp = "+" + temp;
}
if (temp[temp.size() - 1] == 'x') {
temp += "^1";
}
for (int i = 0; i < temp.size(); i++) { //for pol and pow
if ((temp[i] == '+' || temp[i] == '-') && temp[i + 1] == 'x') { //"-x" || "+x"
temp.insert(i + 1, 1, '1');
}
if ((temp[i + 1] == '+' || temp[i + 1] == '-') && temp[i] == 'x') { //"x-" || "x+"
temp.insert(i + 1, "^1");
}
}
bool x_here = false;
for (int i = temp.size() - 1; i >= 0; i--) { //for constant
if (temp[i] == 'x') {
x_here = true;
}
if ((temp[i] == '+' || temp[i] == '-') && x_here == false) { //if we get a '+' or '-' without any 'x' after them(we read it from the tail), the last term is a constant
temp += "x^0";
}
}
//make struct t
for (int i = 0; i < temp.size(); i++) {
if (temp[i] == '+' || temp[i] == '-') {
int positive;
if (temp[i] == '+') {
positive = 1;
} else {
positive = -1;
}
string temp_int; //store the num_char
//for coefficient
int j;
for (j = i + 1; j < temp.size(); j++) {
if (temp[j] == 'x') {
break;
}
temp_int.push_back(temp[j]);
}
int pol_sum = 0;
int pow_sum = 0;
for (int k = 0; k < temp_int.size(); k++) {
pol_sum = pol_sum * 10 + temp_int[k] - '0';
}
temp_int.clear();
//for power
for (int k = j + 2; k < temp.size(); k++) {
if (temp[k] == '+' || temp[k] == '-') {
break;
}
temp_int.push_back(temp[k]);
}
for (int k = 0; k < temp_int.size(); k++) {
pow_sum = pow_sum * 10 + temp_int[k] - '0';
}
t.push_back(term(positive * pol_sum, pow_sum));
}
}
}
void f_d() { //the first derivative of the polynomial
int zero_counter = 0;
for (int i = 0; i < t.size(); i++) {
t[i].pol *= t[i].pow;
t[i].pow--;
if (t[i].pol == 0) {
zero_counter++;
}
}
if (zero_counter == t.size()) { //if all terms are zeros
cout << '0' << endl << '0' << endl << '0' << endl << '0' << endl;
is_end = true;
return;
} else {
for (int i = 0; i < t.size(); i++) {
if (t[i].pol) {
if (t[i].pol > 0 && i) {//not at head and positive
cout << '+';
}
if (t[i].pow == 0) {
cout << t[i].pol;
continue;
}
if (t[i].pol == -1) {
cout << '-';
}
if (t[i].pol != 1 && t[i].pol != -1) {
cout << t[i].pol;
}
if (t[i].pow) {
cout << 'x';
if (t[i].pow != 1) {
cout << '^' << t[i].pow;
}
}
}
}
cout << endl;
}
}
void calculate() {
int num_counter = 0;
//output3
for (int i = 0; i < t.size(); i++) {
if (t[i].pol) {
num_counter++; //the num of term whose coefficient is not zero
if (t[i].pol > 0 && i) {
cout << '+';
}
if (t[i].pow == 0) {
cout << t[i].pol;
continue;
}
if (t[i].pol == -1) {
cout << '-';
}
if (t[i].pol != 1 && t[i].pol != -1) {
cout << t[i].pol;
}
if (t[i].pow) {
cout << '(' << x_v << ')';
if (t[i].pow != 1) {
cout << '^' << t[i].pow;
}
}
}
}
cout << endl;
//calculate
long long int ans[20];
long long int sum = 0;
memset(ans, 0, sizeof(ans));
for (int i = 0; i < t.size(); i++) {
ans[i] = t[i].pol * (long long int)pow((double)x_v, (double)t[i].pow);
sum += ans[i];
}
//output4
for (int i = 0; i < num_counter; i++) {
if (ans[i] >= 0 && i) {
cout << '+';
}
cout << ans[i];
}
cout << endl;
//output5
cout << sum << endl;
}
int main() {
int n;
cin >> n;
for (int nn = 1; nn <= n; nn++) {
t.clear();
is_end = false;
cout << "POLYNOMIAL " << nn << endl;
input();
f_d();
if (is_end) {
continue;
}
calculate();
}
return 0;
}