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 - x^4 + 6x^3 - 2x^2 - 2x + 7
7x^7 - 9x^4 - 9x^2
7x^6 + 6x^5 - 7x^4 - 7x^3 - 9x^2
x^6 + 8x^5 + 4x^4 + 9x^3 - 3x^2 + 6x + 5
6x^8 + 9x^7 - 5x^5 - 8x^4 + 8x^3 + 9x + 3
8x^8 + 9x^7 - 5x^6 + x^5 - x^4 + 6x^3 - 5x^2 + 8x + 3
4x^8 - 4x^7 + 4x^6 - 3x^4 - x^2 + 8x + 4
6x^8 + 9x^7 - 5x^6 + 7x^5 - x^4 + 3x^3 - 6x^2 + 7x + 4
Code: Select all
#include <stdio.h>
#include <math.h>
int main()
{
const int MAX_CO=9;
int poly[MAX_CO];
int count=1,power;
int i;
while(scanf("%d%d%d%d%d%d%d%d%d",&poly[0],&poly[1],&poly[2],&poly[3],&poly[4],&poly[5],&poly[6],&poly[7],&poly[8])==9)
{
count = 1;
for(i=0;i<MAX_CO;i++)
{
power = MAX_CO - i -1;
/*last number which has no 'x' and no power*/
if(power == 0)
{
/*if it's the first number that appears (all number before are zero*/
if(count==1)
{
printf("%d",poly[i]);
continue;
}
/*if it's not the first number that appears*/
if(poly[i]>0) /*positive*/
{
printf(" + ");
printf("%d",poly[i]);
}
else if(poly[i]<0) /*negative*/
{
printf(" - ");
printf("%d",0-poly[i]);
}
continue;
}
/*first number that appears*/
if((count == 1)&&(poly[i] != 0))
{
if(poly[i]>1)
{
printf("%dx",poly[i],power);
}
else if(poly[i]<-1)
{
printf("%dx",poly[i],power);
}
else if(poly[i] == 1)
{
printf("x");
}
else if(poly[i] == -1)
{
printf("-x");
}
/*write out its power*/
if((power != 0)&&(power !=1))
{
printf("^%d",power);
}
count++;
continue;
}
/*second number and so on*/
if((count>1)&&(poly[i] != 0)&&(power !=0))
{
if(poly[i]>0)
{
printf(" + ");
}
else
{
printf(" - ");
}
if(poly[i]>1) /*positive*/
{
printf("%dx",poly[i]);
}
else if((poly[i] == 1)||(poly[i] == -1))
{
printf("x");
}
else if(poly[i]<-1) /*negative*/
{
printf("%dx",0-poly[i]);
}
else {}
}
/*write out the power*/
if((power != 0)&&(power !=1)&&(poly[i] != 0))
{
printf("^%d",power);
}
}
}
return 0;
}
Code: Select all
Got Accepted. Thanks to Roby.
Code: Select all
#include<stdio.h>
int main()
{
int coeff[10];
int i,k;
while(scanf("%d",&i)==1)
{
coeff[8]=i;
for(i=0;i<8;i++)
scanf("%d",&coeff[7-i]);
k=0;
for(i=8;i>1;i--)
{
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
if(coeff[i]!=1)
printf("%dx^%d",coeff[i],i);
else
printf("x^%d",i);
k=1;
}
else if(coeff[i]<0)
{
if(k==0)
printf("-");
else
printf(" - ");
coeff[i]=coeff[i]*(-1);
if(coeff[i]!=1)
printf("%dx^%d",coeff[i],i);
else
printf("x^%d",i);
k=1;
}
}
if(coeff[1]!=0)
{
i=1;
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
if(coeff[i]!=1)
printf("%dx",coeff[i]);
else
printf("x");
k=1;
}
else if(coeff[i]<0)
{
if(k==0)
printf("-");
else
printf(" - ");
coeff[i]=coeff[i]*(-1);
if(coeff[i]!=1)
printf("%dx",coeff[i]);
printf("x");
k=1;
}
}
if(coeff[0]!=0)
{
i=0;
if(coeff[i]>0)
{
if(k==1)
printf(" + ");
printf("%d",coeff[i]);
k=1;
}
else if(coeff[i]<0)
{
if(k==0)
printf("-");
else
printf(" - ");
coeff[i]=coeff[i]*(-1);
printf("%d",coeff[i]);
k=1;
}
}
if(k==0)
printf("0");
printf("\n");
}
return 0;
}
Code: Select all
0 0 0 1 22 -333 0 1 -1
-1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 -7 30 66
0 0 0 0 0 0 1 -3 0
0 0 0 0 0 -1 1 3 -1
-5 0 0 0 -243 0 0 0 -9
-0 -1 -1 -1 -1 -1 -1 -1 -1
-1999999999 -978456 0 0 0 0 56 -89 8
0 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 -1 -1
0 0 0 0 0 0 0 1 -1
0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 -1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 -10
0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 -2 0
0 0 0 0 0 0 0 10 -2
Code: Select all
x^5 + 22x^4 - 333x^3 + x - 1
-x^8
0
-7x^2 + 30x + 66
x^2 - 3x
-x^3 + x^2 + 3x - 1
-5x^8 - 243x^4 - 9
-x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
27649x^8 + 4584x^7 + 56x^2 - 89x + 8
-1
1
x
-x
x + 1
-x + 1
-x - 1
x - 1
1
x^8
x^7
-x^6
2x^8
2
-10
10x
-2x
10x - 2
Code: Select all
x^5 + 22x^4 - 333x^3 + x - 1
-x^8
0
-7x^2 + 30x + 66
x^2 - 3xx
-x^3 + x^2 + 3x - 1
-5x^8 - 243x^4 - 9
-x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
27649x^8 + 4584x^7 + 56x^2 - 89xx + 8
-1
1
x
-x
x + 1
-x + 1
-x - 1
x - 1
1
x^8
x^7
-x^6
2x^8
2
-10
10x
-2xx
10x - 2