2013年8月7日 星期三

2013/8/7 POJ 1555 Polynomial Showdown

 /* http://poj.org/problem?id=1555 */
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int num[8];
int main()
{
    while( scanf("%d", &num[8])!=EOF )
    {
        for(int i = 7; i >= 0; i--) scanf("%d", &num[i]);

        int flag = 1;
        for(int i = 8; i >= 0; i--)
            if( num[i] != 0 ) flag = 0;


        if( flag == 1 ){ printf("0\n"); continue; }

        int f = 0;



        for(int k = 8; k >=0;k--)
        {
            if( num[k] == 0 ) continue;
            else if( num[k] > 0 )
            {
                if( f == 1 ) printf(" + ");
                if( num[k] != 1 || k == 0 ) printf("%d", num[k]);
                if( k != 0 )printf("x");
                if( k != 0 && k != 1 ) printf("^%d", k);
            }
            else
            {
                if( f==0)
                    printf("-");
               else{  printf(" - ");  }
                if( num[k] != -1 || k == 0 ) printf("%d", -1*num[k]);
                if( k != 0 )    printf("x");
                if( k != 0 && k != 1 ) printf("^%d", k);
            }

            f = 1;

        }

        printf("\n");
    }
}

沒有留言:

張貼留言