POJ 1555 Polynomial showdown

Source: Internet
Author: User

Polynomial showdown
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4494 Accepted: 1554

Description

Given the coefficients of a polynomial from degree 8 to 0, you is to format the polynomial in a readable format with unnecessary characters removed. For instance, given the coefficients 0, 0, 0, 1, a, -333, 0, 1, and-1, you should generate a output line which displays X^5 + 22x^4-333x^3 + x-1.
The formatting rules which must is adhered to is as follows:

1. Terms must appear in decreasing order of degree.

2. Exponents should appear after a caret ' "^".

3. The constant term appears as is only the constant.

4. Only terms with nonzero coefficients should appear, unless all terms has zero coefficients in which case the constant term should appear.

5. The only spaces should is a single space on either side of the binary + and-operators.

6. If the leading are positive then no sign should precede it; A negative leading term should is preceded by a minus sign, as in-7x^2 + 30x + 66.

7. Negated terms should appear as a subtracted unnegated term (with the exception of a negative leading term which should appear as described above). That's, rather than x^2 + -3x, the output should be x^2-3x.

8. The constants 1 and-1 should appear only as the constant term. That is, rather than-1x^3 + 1x^2 + 3x^1-1, the output should appear as-x^3 + x^2 + 3x-1.

Input

The input would contain one or more lines of coefficients delimited by one or more spaces. There is nine coefficients per line, each coefficient being an integer with a magnitude of less than 1000.

Output

The output should contain the formatted polynomials, one per line.

Sample Input

0 0 0 1 22-333 0 1-10 0 0 0 0 0-55 5 0

Sample Output

X^5 + 22x^4-333x^3 + x-1-55x^2 + 5x

Source

Mid-Central USA 1996

AC Code:

#include <iostream> #include <cmath>using namespace Std;int main () {    int polynomial[10];    int I,flag;    while (Cin>>polynomial[0]) {for        (i=1;i<9;++i) {            cin>>polynomial[i];        }        flag=0;        for (i=0;i<9;++i) {            if (Polynomial[i]) {                if (flag) cout<< ';                if (flag) cout<< (polynomial[i]>0? ") + ":"- ");                else if (polynomial[i]<0) cout<< "-";                if (ABS (polynomial[i))!=1| | i==8) {                    if (polynomial[i]<0) cout<<-polynomial[i];                    else cout<<polynomial[i];                }                if (i!=8) {                    cout<< "x";                    if (i!=7) cout<< "^" <<8-i;                }                flag++;            }        }        if (flag==0) cout<< "0";        cout<< ' \12 ';    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1555 Polynomial showdown

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.