Monday, May 16, 2016

UVA 392 : Polynomial Showdown - Solution

Unknown
#include<cstdio>
using namespace std;

int main()
{
    int cof[9];
    while (1)
    {
        for (int i=8;i>-1;i--)
        {
            if (scanf("%d",&cof[i])!=1) return 0;
        }
        int flag=1;
        for (int i=8;i>0;i--)
        {
            if (cof[i]!=0)
            {
                if (flag)
                {
                    if (cof[i]==1) printf("x");
                    else if (cof[i]>1)
                    printf("%dx",cof[i],i);
                    if (cof[i]==-1) printf("-x");
                    else if (cof[i]<-1)
                    printf("-%dx",-1*cof[i]);
                    if (i!=1) printf("^%d",i);
                    flag=0;
                }
                else
                {
                    if (cof[i]==1) printf(" + x",i);
                    else if (cof[i]>1)
                    printf(" + %dx",cof[i],i);
                    if (cof[i]==-1) printf(" - x",i);
                    else if (cof[i]<-1)
                    printf(" - %dx",-1*cof[i]);
                    if (i!=1) printf("^%d",i);
                }
            }
        }
        if (flag)
        {
            printf("%d",cof[0]);
        }
        else
        {
            if (cof[0]>0) printf(" + %d",cof[0]);
            else if (cof[0]<0) printf(" - %d",-1*cof[0]);
        }
        printf("\n");
    }
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,