Tuesday, 15 May 2012

UVa 392 Polynomial Showdown Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long




int main()
{
    int coefficients[10],start,i;
    while(cin>>coefficients[0])
    {
        for(i=1;i<9;i++)
            cin>>coefficients[i];

        start=8;
        for(i=0;i<9;i++)
            if(coefficients[i]!=0)
                {
                    start=i;
                    break;
                }

        if(start==8)
            cout<<coefficients[start];
        else
        {
            if(coefficients[start]==-1)
                cout<<"-";
            else if(coefficients[start]==1)
                cout<<"";
            else
                cout<<coefficients[start];
            cout<<"x";
            if(start!=7)
                cout<<"^"<<8-start;

            for(i=start+1;i<9;i++)
                if(coefficients[i]!=0)
                    {
                        if(coefficients[i]<0)
                            cout<<" - ";
                        else
                            cout<<" + ";

                        if((coefficients[i]!=-1 && coefficients[i]!=1) || i==8)
                            {

                                if(coefficients[i]<0)
                                    cout<<coefficients[i]*(-1);
                                else
                                    cout<<coefficients[i];
                            }

                        if(i!=8)
                            cout<<"x";
                        if(i!=7 && i!=8)
                            cout<<"^"<<8-i;
                    }
        }
        cout<<endl;

    }
return 0;
}

No comments:

Post a Comment