The errant physicist (careless physicist)

Source: Internet
Author: User
Problem
Problem

The well-known physicist Alfred E pneumonia is working on problems that involve multiplying polynomials of X and Y. For example, he may need to calculate
The famous physicist Newman: FabricatedPerson Name ) In the problem being studied, We need to calculate the product of the polynomial, which may need to calculate:

(-X8y + 9x3-1) · (x5y + 1 + X3)

Getting the answer
Calculation Result:

-X13y2-x11y + 8x8y + 9x6-x5y + x5y2 + 8x3 + x3y-1 + Y

Unfortunately, such problems are so trivial that the great man's mind keeps drifting off the job, and he gets the wrong answers. as a consequence, several nuclear warheads that he has designed have detonated prematurely, wiping out five major cities and a couple of rain forests.
Unfortunately, this problem is too annoying, and the niub physicist ran a train in his mind while solving the problem, and the result was wrong. The consequences are very serious. Several nuclear weapons will be launched ahead of schedule and several large cities and large-area rain forests will be flattened.

You are to write a program to perform such multiplications and save the world.
You designProgramExecute this product operation to save the world.

 

Input
Input

The file of input data will contain in pairs of lines, with each line containing no more than 80 characters. the final line of the input file contains a # as its first character. each input line contains a polynomial written without spaces and without any explicit exponentiation operator. exponents are positive non-zero unsigned integers. coefficients are also integers, but may be negative. both exponents and coefficients are less than or equal to 100 in magntasks. each term contains at most one factor in X and one in Y.
Each group of input data contains no more than 80 letters. The last line uses # as its first letter. Each input polynomial data does not contain any spaces or power operators. An unsigned positive number with a non-zero index. The coefficient is also an integer, but it may be negative. The order of magnitude of the index and coefficient is smaller than or equal to 100. Each item in a polynomial can have at most two factors X and Y.

 

Output
Output

Your program must multiply each pair of polynomials in the input, and print each product on a pair of lines, the first line containing all the exponents, suitably positioned with respect to the rest of the information, which is in the line below.
You need to write a program to calculate the product of each two input polynomials and print the result in two rows. The first line prints all the indexes, and the position must match the following line.

The following rules control the output format:
The output must comply with the following format requirements.

  1. Terms in the output line must be sorted in decreasing order of powers of x and, for a given power of X, in increasing order of powers of Y.
    The output items must be sorted in descending order of the X index. If they are equal, they are sorted in ascending order of the Y index.
  2. Like terms must be combined into a single term. For example, 40x2y3-38x2y3 is replaced by 2x2y3.
    The same items must be merged. For example, 40x2y3-38x2y3 should be merged into 2x2y3.
  3. Terms with a zero coefficient must not be displayed.
    Do not print the 0 coefficient.
  4. Coefficients of 1 are omitted, cannot for the case of a constant term of 1.
    Do not print the coefficient 1 unless it is a constant.
  5. Exponents of 1 are omitted.
    Ignore the index as 1.
  6. Factors of x0 and Y0 are omitted.
    If the index of a factor (X or Y) is 0, this factor is ignored.
  7. Binary pluses and minuses (that is the pluses and minuses connecting terms in the output) have a single blank column both before and after.
    The plus and minus signs (that is, the plus and minus signs in the middle of the two items) must have spaces before and after the binary operators.
  8. If the coefficient of the first term is negative, it is preceded by a unary minus In the first column, with no intervening blank column. Otherwise, the coefficient itself begins in the first output column.
    If the coefficient of the first item is negative, print the negative number Of The unary operator at the beginning of the line without spaces. If the coefficient of the first item is positive, the item is printed directly from the beginning of the row.
  9. The output can be assumed to fit into a single line of at most 80 characters in length.
    The output may not exceed 80 characters in a row.
  10. There shoshould be no blank lines printed between each pair of output lines.
    Do not enter blank rows between two output rows.
  11. The pair of lines that contain a product shoshould be the same length -- trailing blanks shoshould appear after the last non-blank character of the shorter line to achieve this.
    The product results of the two output rows should have the same length. A shorter row should be filled with spaces at the end of the row to align it.

 

Sample Input
Input example

-Yx8 + 9x3-1 + Y
X5y + 1 + X3
1
1
#

 

Sample output
Output example

13 2 11 8 6 5 5 2 3 3
-X y + 8x y + 9x-x y + 8x + x y-1 + Y

1

 

Analysis
Analysis

There is no good analysis, and it is very boring. Just do it as required. I think it takes so long to translate so much and write so much.CodeNot worth it!

Only pay attention to the case where the processing coefficient is 0 and the product is 0.

 

Solution
Answer

# Include <algorithm> # include <iostream> # include <string> # include <vector> # include <stdio. h> using namespace STD; // represents the struct term of each struct In the polynomial {// The members are coefficients, X and Y indexes int COF; int Xe; int ye; // constructor, initialize the variable term (int c, int X, int y) by parameters: COF (C), Xe (x), Ye (y ){}}; // compare the values of the two indexes for sorting and merging bool greaterterm (const term & T1, const term & T2) {return (t1.xe> t2.xe | (t1.xe = t2.xe & t1.ye <t2.ye);} // parse the void function of the input polynomial string Parsepolynomial (char * pstr, vector <term> & Terms) {// process each item in a loop for (INT nnum; * pstr! = 0;) {// determine the positive and negative numbers of the item and initialize the term (* pstr = '-'? -1: 1, 0, 0); // if the front is signed, then the pointer is shifted back to pstr + = (* pstr = '-' | * pstr = '+ ')? 1: 0; // If the coefficient is 0, the entire if (* pstr = '0') {for (++ pstr; * pstr! = '\ 0' & * pstr! = '+' & * Pstr! = '-'; ++ Pstr); continue;} // read the coefficient after the symbol for (nnum = 0; isdigit (* pstr ); nnum = nnum * 10 + * pstr ++-'0'); // If the coefficient is not 0, it is multiplied to the coefficient of the item struct (Retain the original symbol) for (term. COF * = (nnum = 0 )? 1: nnum; isalpha (* pstr);) {// read the pointer of two variables cyclically (if any ), are you sure you want to use the X or Y index int * Pe = (* pstr = 'X ')? & Term. xe: & term. ye; // read the following index for (; isdigit (* ++ pstr); * Pe = * Pe * 10 + * pstr-'0 '); // if there is no index, the index is 1 * Pe = (* Pe = 0 )? 1: * PE;} // Add the new item struct to the array terms. push_back (TERM) ;}/// main function int main (void) {// read all input data cyclically. If the # sign ends for (string str1, str2; cin> str1 & str1! = "#";) {CIN> str2; If (str1.empty () | str2.empty () continue; const int nmaxlen = 100; char szbuf1 [nmaxlen], szbuf2 [nmaxlen]; vector <term> poly1, poly2, result; // stores two strings for parsing the polynomial strcpy (szbuf1, str1.c _ STR (); strcpy (szbuf2, str2.c _ STR (); // parse two polynomial strings parsepolynomial (szbuf1, poly1); parsepolynomial (szbuf2, poly2); vector <term >:: iterator I, J; // execute polynomial multiplication for (I = poly1.begin (); I! = Poly1.end (); ++ I) {for (j = poly2.begin (); J! = Poly2.end (); ++ J) {Term term (I-> COF * j-> COF, I-> Xe + J-> XE, i-> ye + J-> ye); result. push_back (TERM) ;}// sort (result. begin (), result. end (), greaterterm); fill (& szbuf1 [0], & szbuf1 [nmaxlen], ''); fill (& szbuf2 [0], & szbuf2 [nmaxlen], ''); int NPOs = 0; // search for similar items for (I = result. begin (); I! = Result. end (); ++ I) {// merge the same category items (if any) for (j = I + 1; j <result. end () & I-> Xe = J-> Xe & I-> ye = J-> ye ;) {I-> COF + = J-> COF; j = result. erase (j);} // If the coefficient of this item is not 0, output if (I-> COF! = 0) {If (NPOs> 0) {// It is not the first item, and the intermediate operator ++ NPOs is output; // The space above the output operator szbuf2 [NPOs ++] = I-> COF> 0? '+': '-'; Szbuf2 [NPOs ++] = '';} else {// first entry, output the previous symbol (if it is negative) szbuf2 [0] = '-'; NPOs + = (I-> COF <0);} // If the coefficient (absolute value) is not 1 or the XY index is 0, the output coefficient is I-> COF = ABS (I-> COF); if (I-> COF! = 1 | (I-> Xe = 0 & I-> ye = 0) {NPOs + = sprintf (& szbuf2 [NPOs], "% d ", i-> COF); // wipe sprintf's ass szbuf2 [NPOs] = '';} // If the X index is not 0, the output xif (I-> Xe> 0) {szbuf2 [NPOs ++] = 'X'; if (I-> Xe> 1) {NPOs + = sprintf (& szbuf1 [NPOs], "% d ", i-> Xe); szbuf1 [NPOs] = ''; }}// same as if (I-> ye> 0) {szbuf2 [NPOs ++] = 'y '; if (I-> ye> 1) {NPOs + = sprintf (& szbuf1 [NPOs], "% d", I-> ye ); szbuf1 [NPOs] = '';}}}// if no item is output, the polynomial product is 0if (NPOs = 0) {szbuf2 [NPOs ++] = '0';} // end of the polynomial product string and output szbuf1 [NPOs] = szbuf2 [NPOs] = '\ 0 '; cout <szbuf1 <'\ n' <szbuf2 <Endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.