Tuesday, October 25, 2011

[UVa] 392 - Polynomial Showdown

Just conditioning, loads of ifs

If you're getting PE or WA just test with some isolated values for different coefficients. Some of my test cases are.
0    0    0    1   22 -333    0    1   -1
0    0    0    0    0    0  -55    5    0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 -1 0 0
0 0 0 0 0 -1 0 0 0
0 0 0 0 -1 0 0 0 0
0 0 0 -1 0 0 0 0 0
0 0 -1 0 0 0 0 0 0
0 -1 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
x^5 + 22x^4 - 333x^3 + x - 1
-55x^2 + 5x
0
-1
-x
-x^2
-x^3
-x^4
-x^5
-x^6
-x^7
-x^8
0


#include <stdio.h>
#include <string.h>

int a[10];

int main()
{
    int i, print, z;
    //freopen("input.txt","r+",stdin);
    //freopen("output.txt","w+",stdout);
    while (scanf("%d %d %d %d %d %d %d %d %d",&a[8],&a[7],&a[6],&a[5],&a[4],&a[3],&a[2],&a[1],&a[0])==9)
    {
        for (i=0, z=1 ; i<9 && z; i++)      //-----|Checking if the
            if (a[i]) z=0;                  //-----|polynomial is zero

        if (z) {                            //-----|The polynomial is found
            printf("0\n");                  //-----|to be zero in fact
            continue;
        }

    //-[ The first value of the equation is not printed yet ]
        print=0;
        for (i=8 ; i>1 ; i--)
        {
            if (print && a[i]) printf(" ");

            if (a[i]==-1)
            {
                (print? printf("- x^%d",i) : printf("-x^%d",i) );
            } else if (a[i]==1)
            {
                (print? printf("+ x^%d",i) : printf("x^%d",i) );
            } else if (a[i]<0)
            {
                (print? printf("- %dx^%d",-1*a[i],i) : printf("%dx^%d",a[i],i) );
            } else if (a[i])
            {
                (print? printf("+ %dx^%d",a[i],i) : printf("%dx^%d",a[i],i) );
            }
        //-- [ From the next time a space will seperate characters ]
            if(a[i]) print=1;
        }
        if (a[i])
        {
            if (print && a[i]) printf(" ");
            if (a[i]==-1)
            {
                (print? printf("- x") : printf("-x") );
            } else if (a[i]==1)
            {
                (print? printf("+ x") : printf("x") );
            } else if (a[i]<0)
            {
                (print? printf("- %dx",-1*a[i]) : printf("%dx",a[i]) );
            } else if (a[i])
            {
                (print? printf("+ %dx",a[i]) : printf("%dx",a[i]));
            }
        //-- [ From the next time a space will seperate characters ]
            if(a[i]) print=1;
        }
        i--;
        if (a[i])
        {
            if (print && a[i]) printf(" ");
            if (a[i]<0)
            {
                (print ? printf("- %d",-1*a[i]) : printf("%d",a[i]) );
            } else if (a[i])
            {
                (print ? printf("+ %d",a[i]) : printf("%d",a[i]) );
            }
        }

        printf("\n");
    }
    return 0;
}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...